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

feat: Implement cast typecheck and diagnostics #17984

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

ShoyuVanilla
Copy link
Member

@ShoyuVanilla ShoyuVanilla commented Aug 28, 2024

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 28, 2024
m_cast: Mutability,
table: &mut InferenceTable<'_>,
) -> Result<(), CastError> {
// Mutability order is opposite tu rustc. `Mut < Not`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to change that on our side then (separately from this), the less we diverge from rustc with things the better we can draw comparisons between the code bases (which imo for type check is quite important)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But this type is from chalk_ir. Maybe we should change this at there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, another typo "tu" here 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, probably no point in changing it then. But we should keep that in mind for the trait solver migration 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are many places in our code that tailored for chalk quirks and I can remember some of them. The ones that no one remembers or knows would be covered by our tests, I hope 😨

@ShoyuVanilla
Copy link
Member Author

I was blocked on mir evaluation failures when I push cast adjustments into InferenceResult and finally found the reason 🥳 our mir castkind is doing both mir and hir_typeck parts of rustc and thus pushing adjustment makes erroneous duplication.
I'll continue on this tommorrow

@ShoyuVanilla
Copy link
Member Author

So, this will also fix #16564 when finished

@@ -278,7 +282,7 @@ fn main() {
"#,
r#"
unsafe fn func() {
let x = &5 as *const usize;
let x = &5_usize as *const usize;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous cast &5 as *const usize is illegal

@@ -399,7 +399,7 @@ extern "C" {
fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32;
}

fn my_cmp(x: &[u8], y: &[u8]) -> i32 {
fn my_cmp(x: &[u8; 3], y: &[u8; 3]) -> i32 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous cast &[u8] as *const u8 is invalid in rustc
image

crates/hir-def/src/data/adt.rs Show resolved Hide resolved
Comment on lines +910 to +911
// Structs can have DST as its last field and such cases are not handled
// as unsized by the chalk, so we do this manually
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not sure how relevant this is but) this reminds me, we do not populate chalk's AdtvariantDatum's fields right now as that slows down things by quite a bit. Just letting you know so you are aware (the relevant part for that is stubbed out at the moment even though we have it implemented)

let _variant_id_to_fields = |id: VariantId| {
let variant_data = &id.variant_data(db.upcast());
let fields = if variant_data.fields().is_empty() {
vec![]
} else {
let field_types = db.field_types(id);
variant_data
.fields()
.iter()
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst))
.filter(|it| !it.contains_unknown())
.collect()
};
rust_ir::AdtVariantDatum { fields }
};
let variant_id_to_fields = |_: VariantId| rust_ir::AdtVariantDatum { fields: vec![] };

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was wondering why chalk is saying struct Foo { inner: [u8] } is Sized and that was the answer😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc it added like 10 seconds to inference on self which is a bit much + at the time we couldnt inspect third party deps of the standard library (which we soon can) so we could reconsider if this turns out to cause troubles

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 that case we might omit it and consider it as sized; that will make some false negative cast errors but we'll not have irritating false positives anyway (though we should change function name into somewhat like maybe_sized to avoid possible footguns in future that might be caused by trusting that function will always return the correct value 😅 )

@Veykril
Copy link
Member

Veykril commented Sep 3, 2024

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Sep 3, 2024

📌 Commit d186bdc has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Sep 3, 2024

⌛ Testing commit d186bdc with merge 6e84451...

@bors
Copy link
Collaborator

bors commented Sep 3, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 6e84451 to master...

@bors bors merged commit 6e84451 into rust-lang:master Sep 3, 2024
11 checks passed
@ShoyuVanilla ShoyuVanilla deleted the cast branch September 3, 2024 06:17
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.

No diagnostic reported for non-primitive casts Incorrect "cannot move &mut () out of reference" error
4 participants