Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ShoyuVanilla
Copy link
Member

Fixes #19431

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 23, 2025
let t: *mut (dyn Trait + 'static) = 0 as *mut _;
//^^^^^^^^^^^ error: cannot cast `usize` to a fat pointer `*mut _`

Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Mar 23, 2025

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

// 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 _
Copy link
Member Author

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:

/// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive cast diagnostics with DSTs and zerocopy
2 participants