-
Notifications
You must be signed in to change notification settings - Fork 1.8k
internal: add_explicit_type
respects coercions
#9552
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
Conversation
#[test] | ||
fn add_explicit_type_inserts_coercions() { | ||
// This should set `*const [u32]` | ||
check_assist( | ||
add_explicit_type, | ||
r#" | ||
//- minicore: coerce_unsized | ||
fn f() { | ||
let $0x: *const [_] = &[3]; | ||
} | ||
"#, | ||
r#" | ||
fn f() { | ||
let x: *const [_] = &[3]; | ||
} | ||
"#, | ||
); | ||
} |
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.
So for some reason the coercions here are all slices of type variables instead of a conrete type(even if using a 3u32
literal, forgot to add that to the commit). Is this expected?
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.
Oh, we need to resolve the types in adjustments after inference, see this function: https://github.com/rust-analyzer/rust-analyzer/blob/6f3a3bdde4e526d880b9d8d9c613d2cef50a5f3b/crates/hir_ty/src/infer.rs#L390
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.
Oooh ye that totally makes sense. Though out of interest why do we lose the concrete type information for the array/slice there even if it is known before the coercion? I would expect that to persist in this specific case.
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.
The type we coerce to is *const [_]
, which contains a type variable. The type variable gets unified with the u32
, but to actually see that we have to resolve it in the end.
bors r+ |
@@ -407,6 +407,12 @@ impl<'a> InferenceContext<'a> { | |||
for (_, subst) in result.method_resolutions.values_mut() { | |||
*subst = self.table.resolve_completely(subst.clone()); | |||
} | |||
for adjustment in result.expr_adjustments.values_mut().flatten() { |
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 can avoid the clones here and above using mem::replace
, do you think it's worth it?
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.
No, cloning Chalk types is cheap.
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.
Yeah, I suppose we'd still have to intern (or at least clonel the error type.
or so I'd like to say but there is one odd case here where it doesn't work(see review)
Fixes #6107