-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add a storage for the origin of const inference variables. #72328
Comments
It would be good to have some examples of where diagnostics are subpar because of this missing feature, to decide if it's a prerequisite for stabilising |
yeah, will look a bit into some interesting test cases here in the near future hopefully |
One issue here is something like this: #![feature(min_const_generics)]
struct Foo;
impl Foo {
fn bar(self) -> Foo {
Foo
}
fn baz<const N: usize>(self) -> Foo {
println!("baz: {}", N);
Foo
}
}
fn main() {
Foo.bar().bar().bar().bar().baz();
} which results in
for
btw I am not actually sure if this issue makes sense, we already have the origin of const infer values stored, so |
Is there any suggestion for user on how to fix this? |
Not completely sure what you mean here @pickfire Do you mean when the user encounters a
On working on this issue itself, I am not actually sure what this issue actually is about, as my initial reason for opening it was somewhat incorrect. I do think we should further improve the error message when type inference gets stuck for const inference variables. I don't know the best way to do this though, so I can't really give any mentoring instructions here yet |
I think the main reason this issue exists is that when type inference fails for type parameters, we already have lots of heuristics to suggest to the user how to correct it. We don't have any of those for consts yet. However, this might be something that we continue to improve over time, so there may not be any direct action we can take with regards to this issue now. |
Yes, I don't know how to solve const issue but it would be good to have some sort of guidance. |
I've opened #76737 for the turbofish suggestion. Let's simply open new issues as we encounter problems, and then we can close this one. |
improve const infer error cc rust-lang#72328 reduces it from ``` error[E0282]: type annotations needed --> src/main.rs:17:5 | 17 | Foo.bar().bar().bar().bar().baz(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: unable to infer the value of a const parameter ``` to ``` error[E0282]: type annotations needed --> $DIR/method-chain.rs:21:33 | LL | Foo.bar().bar().bar().bar().baz(); | ^^^ | = note: cannot infer the value of the const parameter `N` ``` r? @varkor
improve const infer error cc rust-lang#72328 reduces it from ``` error[E0282]: type annotations needed --> src/main.rs:17:5 | 17 | Foo.bar().bar().bar().bar().baz(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: unable to infer the value of a const parameter ``` to ``` error[E0282]: type annotations needed --> $DIR/method-chain.rs:21:33 | LL | Foo.bar().bar().bar().bar().baz(); | ^^^ | = note: cannot infer the value of the const parameter `N` ``` r? @varkor
We currently don't store the origin of const inference variables. This is needed to emit more helpful error messages.
For type inference variables, this is done using https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/struct.InferCtxtInner.html#method.type_variables and then
https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/type_variable/struct.TypeVariableTable.html#method.var_origin.
We use this to provide better diagnostics in case type inference fails:
rust/src/librustc_infer/infer/error_reporting/need_type_info.rs
Lines 198 to 201 in 9912925
A similar mechanism should probably also be used for consts.
The text was updated successfully, but these errors were encountered: