@@ -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
} ;
@@ -32,7 +32,8 @@ use rustc_middle::ty::{
32
32
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
33
33
use rustc_span:: { symbol:: sym, Span , Symbol } ;
34
34
use 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 } ;
36
37
use std:: collections:: VecDeque ;
37
38
38
39
declare_clippy_lint ! {
@@ -1362,7 +1363,12 @@ fn replace_types<'tcx>(
1362
1363
let projection = cx. tcx
1363
1364
. mk_projection ( assoc_item. def_id , cx. tcx . mk_substs_trait ( new_ty, [ ] ) ) ;
1364
1365
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)
1366
1372
&& substs[ term_param_ty. index as usize ] != ty:: GenericArg :: from ( projected_ty)
1367
1373
{
1368
1374
deque. push_back ( ( * term_param_ty, projected_ty) ) ;
0 commit comments