Skip to content

Commit 6ff9f64

Browse files
Merge pull request #21241 from ShoyuVanilla/issue-20487-again
fix: Support dyn compatibility for old toolchains without `MetaSized`
2 parents 186e3fe + b4ad7da commit 6ff9f64

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

crates/hir-ty/src/dyn_compatibility.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,14 @@ fn receiver_is_dispatchable<'db>(
427427
};
428428

429429
let meta_sized_did = lang_items.MetaSized;
430-
let Some(meta_sized_did) = meta_sized_did else {
431-
return false;
432-
};
430+
431+
// TODO: This is for supporting dyn compatibility for toolchains doesn't contain `MetaSized`
432+
// trait. Uncomment and short circuit here once `MINIMUM_SUPPORTED_TOOLCHAIN_VERSION`
433+
// become > 1.88.0
434+
//
435+
// let Some(meta_sized_did) = meta_sized_did else {
436+
// return false;
437+
// };
433438

434439
// Type `U`
435440
// FIXME: That seems problematic to fake a generic param like that?
@@ -450,17 +455,16 @@ fn receiver_is_dispatchable<'db>(
450455
});
451456
let trait_predicate = TraitRef::new_from_args(interner, trait_.into(), args);
452457

453-
let meta_sized_predicate =
454-
TraitRef::new(interner, meta_sized_did.into(), [unsized_self_ty]);
458+
let meta_sized_predicate = meta_sized_did
459+
.map(|did| TraitRef::new(interner, did.into(), [unsized_self_ty]).upcast(interner));
455460

456461
ParamEnv {
457462
clauses: Clauses::new_from_iter(
458463
interner,
459-
generic_predicates.iter_identity_copied().chain([
460-
unsize_predicate.upcast(interner),
461-
trait_predicate.upcast(interner),
462-
meta_sized_predicate.upcast(interner),
463-
]),
464+
generic_predicates
465+
.iter_identity_copied()
466+
.chain([unsize_predicate.upcast(interner), trait_predicate.upcast(interner)])
467+
.chain(meta_sized_predicate),
464468
),
465469
}
466470
};

crates/hir-ty/src/tests/regression/new_solver.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,62 @@ fn debug(_: &dyn Foo) {}
226226
227227
impl Foo for i32 {}
228228
229+
fn main() {
230+
debug(&1);
231+
}"#,
232+
);
233+
234+
// toolchains <= 1.88.0, before sized-hierarchy.
235+
check_no_mismatches(
236+
r#"
237+
#[lang = "sized"]
238+
pub trait Sized {}
239+
240+
#[lang = "unsize"]
241+
pub trait Unsize<T: ?Sized> {}
242+
243+
#[lang = "coerce_unsized"]
244+
pub trait CoerceUnsized<T: ?Sized> {}
245+
246+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
247+
248+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
249+
250+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
251+
252+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
253+
254+
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
255+
256+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
257+
258+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
259+
260+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
261+
262+
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
263+
264+
#[lang = "dispatch_from_dyn"]
265+
pub trait DispatchFromDyn<T> {}
266+
267+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
268+
269+
impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
270+
271+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
272+
273+
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
274+
275+
trait Foo {
276+
fn bar(&self) -> u32 {
277+
0xCAFE
278+
}
279+
}
280+
281+
fn debug(_: &dyn Foo) {}
282+
283+
impl Foo for i32 {}
284+
229285
fn main() {
230286
debug(&1);
231287
}"#,

crates/rust-analyzer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate ra_ap_rustc_type_ir as rustc_type_ir;
1414
/// Any toolchain less than this version will likely not work with rust-analyzer built from this revision.
1515
pub const MINIMUM_SUPPORTED_TOOLCHAIN_VERSION: semver::Version = semver::Version {
1616
major: 1,
17-
minor: 90,
17+
minor: 78,
1818
patch: 0,
1919
pre: semver::Prerelease::EMPTY,
2020
build: semver::BuildMetadata::EMPTY,

0 commit comments

Comments
 (0)