-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Yet another false positive invalid cast diagnostic #19432
base: master
Are you sure you want to change the base?
Conversation
let t: *mut (dyn Trait + 'static) = 0 as *mut _; | ||
//^^^^^^^^^^^ error: cannot cast `usize` to a fat pointer `*mut _` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have disabled diagnostics on casts into *const dyn Trait
here
rust-analyzer/crates/hir-ty/src/infer/cast.rs
Lines 125 to 130 in 37acea8
// Chalk doesn't support trait upcasting and fails to solve some obvious goals | |
// when the trait environment contains some recursive traits (See issue #18047) | |
// We skip cast checks for such cases for now, until the next-gen solver. | |
if contains_dyn_trait(&self.cast_ty) { | |
return Ok(()); | |
} |
but this somehow survived it by hiding behinde inference var 😅
struct ZerocopyKnownLayoutMaybeUninit(<<Struct as Field>::Type as KnownLayout>::MaybeUninit); | ||
|
||
fn test(ptr: *mut ZerocopyKnownLayoutMaybeUninit) -> *mut <<Struct as Field>::Type as KnownLayout>::MaybeUninit { | ||
ptr as *mut _ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In zerocopy
's proc macro expansion, there are so many nested projection types and inference vars and because of that the last field extracting stops along the way in the following function:
rust-analyzer/crates/hir-ty/src/infer/unify.rs
Lines 919 to 944 in 37acea8
/// Check if given type is `Sized` or not | |
pub(crate) fn is_sized(&mut self, ty: &Ty) -> bool { | |
let mut ty = ty.clone(); | |
{ | |
let mut structs = SmallVec::<[_; 8]>::new(); | |
// Must use a loop here and not recursion because otherwise users will conduct completely | |
// artificial examples of structs that have themselves as the tail field and complain r-a crashes. | |
while let Some((AdtId::StructId(id), subst)) = ty.as_adt() { | |
let struct_data = self.db.variant_data(id.into()); | |
if let Some((last_field, _)) = struct_data.fields().iter().next_back() { | |
let last_field_ty = self.db.field_types(id.into())[last_field] | |
.clone() | |
.substitute(Interner, subst); | |
if structs.contains(&ty) { | |
// A struct recursively contains itself as a tail field somewhere. | |
return true; // Don't overload the users with too many errors. | |
} | |
structs.push(ty); | |
// Structs can have DST as its last field and such cases are not handled | |
// as unsized by the chalk, so we do this manually. | |
ty = last_field_ty; | |
} else { | |
break; | |
}; | |
} | |
} |
Ideally, this should anyway return !Sized
as the function body before trait solving is just a performance-related logic, but the chalk returns wrong unique solution to a goal struct Dst([u8]): Sized
🤔
So, I added a function eagerly resolves such nested inference vars and projection types.
Fixes #19431