@@ -3,7 +3,7 @@ use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exact
33use clippy_utils:: msrvs:: { self , Msrv } ;
44use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
55use clippy_utils:: sugg:: has_enclosing_paren;
6- use clippy_utils:: ty:: { adt_and_variant_of_res, expr_sig, is_copy, peel_mid_ty_refs, ty_sig} ;
6+ use clippy_utils:: ty:: { adt_and_variant_of_res, expr_sig, implements_trait , is_copy, peel_mid_ty_refs, ty_sig} ;
77use clippy_utils:: {
88 fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, path_to_local, walk_to_expr_usage,
99} ;
@@ -32,7 +32,8 @@ use rustc_middle::ty::{
3232use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
3333use rustc_span:: { symbol:: sym, Span , Symbol } ;
3434use rustc_trait_selection:: infer:: InferCtxtExt as _;
35- use rustc_trait_selection:: traits:: { query:: evaluate_obligation:: InferCtxtExt as _, Obligation , ObligationCause } ;
35+ use rustc_trait_selection:: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
36+ use rustc_trait_selection:: traits:: { Obligation , ObligationCause } ;
3637use std:: collections:: VecDeque ;
3738
3839declare_clippy_lint ! {
@@ -1362,7 +1363,12 @@ fn replace_types<'tcx>(
13621363 let projection = cx. tcx
13631364 . mk_projection ( assoc_item. def_id , cx. tcx . mk_substs_trait ( new_ty, [ ] ) ) ;
13641365
1365- if let Ok ( projected_ty) = cx. tcx . try_normalize_erasing_regions ( cx. param_env , projection)
1366+ // Verify that `new_ty` implements the relevant trait before trying to normalize `projection` to
1367+ // avoid hitting this `span_bug`:
1368+ // https://github.com/rust-lang/rust/blob/695072daa6cc04045f2aa79d751d884ad5263080/compiler/rustc_trait_selection/src/traits/query/normalize.rs#L272-L275
1369+ // See: https://github.com/rust-lang/rust/issues/107877
1370+ if implements_trait ( cx, new_ty, assoc_item. container_id ( cx. tcx ) , List :: empty ( ) )
1371+ && let Ok ( projected_ty) = cx. tcx . try_normalize_erasing_regions ( cx. param_env , projection)
13661372 && substs[ term_param_ty. index as usize ] != ty:: GenericArg :: from ( projected_ty)
13671373 {
13681374 deque. push_back ( ( * term_param_ty, projected_ty) ) ;
0 commit comments