@@ -3,7 +3,7 @@ use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exact
3
3
use clippy_utils:: msrvs:: { self , Msrv } ;
4
4
use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
5
5
use 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} ;
7
7
use clippy_utils:: {
8
8
fn_def_id, get_parent_expr, get_parent_expr_for_hir, is_lint_allowed, path_to_local, walk_to_expr_usage,
9
9
} ;
@@ -1362,7 +1362,12 @@ fn replace_types<'tcx>(
1362
1362
let projection = cx. tcx
1363
1363
. mk_projection ( assoc_item. def_id , cx. tcx . mk_substs_trait ( new_ty, [ ] ) ) ;
1364
1364
1365
- if let Ok ( projected_ty) = cx. tcx . try_normalize_erasing_regions ( cx. param_env , projection)
1365
+ // Verify that `new_ty` implements the relevant trait before trying to normalize `projection` to
1366
+ // avoid hitting this `span_bug`:
1367
+ // https://github.com/rust-lang/rust/blob/695072daa6cc04045f2aa79d751d884ad5263080/compiler/rustc_trait_selection/src/traits/query/normalize.rs#L272-L275
1368
+ // See: https://github.com/rust-lang/rust/issues/107877
1369
+ if implements_trait ( cx, new_ty, assoc_item. container_id ( cx. tcx ) , List :: empty ( ) )
1370
+ && let Ok ( projected_ty) = cx. tcx . try_normalize_erasing_regions ( cx. param_env , projection)
1366
1371
&& substs[ term_param_ty. index as usize ] != ty:: GenericArg :: from ( projected_ty)
1367
1372
{
1368
1373
deque. push_back ( ( * term_param_ty, projected_ty) ) ;
0 commit comments