Skip to content

Commit babfbde

Browse files
committed
Fix infinite obligation loop for Send/Sync
1 parent ca7408b commit babfbde

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

clippy_lints/src/any_coerce.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ fn check_unsize_coercion<'tcx>(
9696
// redo the typechecking for this coercion to see if it required unsizing something to `dyn Any`
9797
// see https://github.com/rust-lang/rust/blob/cae6efc37d70ab7d353e6ab9ce229d59a65ed643/src/librustc_typeck/check/coercion.rs#L454-L611
9898
let tcx = infcx.tcx;
99+
let coerce_unsized_trait_did = tcx.lang_items().coerce_unsized_trait().unwrap();
100+
let unsize_trait_did = tcx.lang_items().unsize_trait().unwrap();
101+
99102
// don't report overflow errors
100103
let mut selcx = traits::SelectionContext::with_query_mode(&infcx, traits::TraitQueryMode::Canonical);
101104
let mut queue = VecDeque::new();
102105
queue.push_back(
103106
ty::TraitRef::new(
104-
tcx.lang_items().coerce_unsized_trait().unwrap(),
107+
coerce_unsized_trait_did,
105108
tcx.mk_substs_trait(src_ty, &[tgt_ty.into()]),
106109
)
107110
.to_poly_trait_ref(),
@@ -120,12 +123,14 @@ fn check_unsize_coercion<'tcx>(
120123
trait_ref.to_poly_trait_predicate(),
121124
));
122125
if let Ok(Some(vtable)) = select_result {
123-
// we only care about trait predicates
126+
// we only care about trait predicates for these traits
127+
let traits = [coerce_unsized_trait_did, unsize_trait_did];
124128
queue.extend(
125129
vtable
126130
.nested_obligations()
127131
.into_iter()
128-
.filter_map(|oblig| oblig.predicate.to_opt_poly_trait_ref()),
132+
.filter_map(|oblig| oblig.predicate.to_opt_poly_trait_ref())
133+
.filter(|tr| traits.contains(&tr.def_id())),
129134
);
130135
}
131136
}

tests/ui/any_coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct Unsizeable<T: ?Sized, U: ?Sized, V: ?Sized> {
2929
}
3030

3131
fn main() {
32-
let mut box_any: Box<dyn Any> = Box::new(Foo);
32+
let mut box_any: Box<dyn Any + Send> = Box::new(Foo);
3333
let _: *mut dyn Any = &mut box_any; // LINT
3434
let _: *mut dyn Any = &mut *box_any; // ok
3535

0 commit comments

Comments
 (0)