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

Fails to identify type of vec![] macro #6837

Closed
zeramorphic opened this issue Dec 11, 2020 · 11 comments · Fixed by #7815
Closed

Fails to identify type of vec![] macro #6837

zeramorphic opened this issue Dec 11, 2020 · 11 comments · Fixed by #7815
Labels
A-ty type system / type inference / traits / method resolution E-has-instructions Issue has some instructions and pointers to code to get started S-actionable Someone could pick this issue up and work on it right now

Comments

@zeramorphic
Copy link

In an empty (latest rust-analyzer, nightly rust) VSCode project, I am getting the following (related) errors to do with the vec! macro.
image
It seems as if rust-analyzer is maybe unable to resolve the second type parameter related to the new allocator API, and that error stops it from filling in the first parameter. Weirdly, it semi-correctly identifies a char in the following image, but thinks that it is part of an array.
image
It also identifies vec!["]; to be a Vec<[&str; _]>.

This is probably not a direct repeat of #6668 - I'm using a nightly version of rust-analyzer released after that issue was patched. Also, using the normal Vec constructor, and adding elements in afterwards, works fine thanks to type inference - it's only a problem with the macro, it seems.

@lnicola lnicola added the A-ty type system / type inference / traits / method resolution label Dec 11, 2020
@flodiebold
Copy link
Member

I can't reproduce this with current rust-analyzer. Can you check whether it's still happening?

@flodiebold flodiebold added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Dec 14, 2020
@zeramorphic
Copy link
Author

Yes, I've just updated rust-analyzer vscode plugin, the language server, and rustup. The issue is still here. If it's relevant, I'm using x86_64-pc-windows-msvc.

@flodiebold
Copy link
Member

Ah wait, I missed that this is with nightly rust.

@zeramorphic
Copy link
Author

Yeah, I'm pretty sure it's to do with the new allocator API adding extra type parameters to Vec.

@flodiebold
Copy link
Member

Ah, vec![1i32] now expands to <[_]>::into_vec(box [1i32]), and we're having problems either with the Box self type or with something about the new allocator parameter.

@flodiebold flodiebold added S-actionable Someone could pick this issue up and work on it right now and removed S-unactionable Issue requires feedback, design decisions or is blocked on other work labels Dec 14, 2020
@Pawel82S
Copy link

I've got the same problem but with stable rust v 1.50

@detrumi
Copy link
Member

detrumi commented Feb 27, 2021

It's the box_syntax feature:

let a = <[_]>::into_vec(Box::new([1i32])); // Vec<i32, Global>
let a = <[_]>::into_vec(box [1i32]); // Vec<{unknown}, {unknown}>

I think RA doesn't figure out the allocator parameter when using box ... syntax:

< [INFO hir_ty::traits] trait_solve_query(Implements(fn new<?0.0>(?0.0) -> Box<?0.0, Global>: Deref))
< [INFO hir_ty::traits] trait_solve_query(Implements(Box<[i32; _], Global>: CoerceUnsized<Box<[?0.0], ?0.1>>))
> [INFO hir_ty::traits] trait_solve_query(Implements(Box<[i32; _], ?0.0>: CoerceUnsized<Box<[?0.1], ?0.2>>))

@flodiebold
Copy link
Member

How does box interact with the allocator parameter? Does it always use the default?

@detrumi
Copy link
Member

detrumi commented Feb 28, 2021

According to rust#58457):

Ideally, inherent methods like Box::new would be applied to
Box<T, A: Alloc + Default>, but as of now, that would be backwards
incompatible because it would require type annotations in places where
they currently aren't required.

So at the moment Box::new always uses std::alloc::Global, but that may change in the future.
box_syntax might already supports custom allocators, and defaults to the Global (like how Vec does for example, but in this case built into the syntax instead of as a default type param). Not sure though, I'll ask around.

@detrumi
Copy link
Member

detrumi commented Feb 28, 2021

Tried it out, and turns out that box_syntax currently always uses std::alloc::Global (playground link)
So I think rust-analyzer has to set that default somewhere, instead of trying to infer it.

@flodiebold
Copy link
Member

Ok, that's probably the reason it doesn't work then. The handling for the box syntax is here:
https://github.com/rust-analyzer/rust-analyzer/blob/803ff2e55eeb6bd1d9028fbbffe34721f0e6d459/crates/hir_ty/src/infer/expr.rs#L509-L512
Currently we just fill it with a type variable; we could instead look up the generic defaults (using db.generic_defaults) and put them there.

@flodiebold flodiebold added the E-has-instructions Issue has some instructions and pointers to code to get started label Feb 28, 2021
@bors bors bot closed this as completed in 72457d0 Feb 28, 2021
@bors bors bot closed this as completed in #7815 Feb 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution E-has-instructions Issue has some instructions and pointers to code to get started S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants