Skip to content

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

Merged
merged 2 commits into from
Jul 10, 2021
Merged

Conversation

Veykril
Copy link
Member

@Veykril Veykril commented Jul 10, 2021

or so I'd like to say but there is one odd case here where it doesn't work(see review)

Fixes #6107

Comment on lines 265 to 281
#[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];
}
"#,
);
}
Copy link
Member Author

@Veykril Veykril Jul 10, 2021

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?

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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.

@Veykril
Copy link
Member Author

Veykril commented Jul 10, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 10, 2021

@bors bors bot merged commit f83f069 into rust-lang:master Jul 10, 2021
@@ -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() {
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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.

@Veykril Veykril deleted the add-ty branch July 10, 2021 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Insert explicit type removes coercion to raw pointer
3 participants