Skip to content

Add a storage for the origin of const inference variables. #72328

@lcnr

Description

@lcnr
Contributor

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:

let ty_vars = &inner.type_variables();
let var_origin = ty_vars.var_origin(ty_vid);
if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind {
let parent_def_id = def_id.and_then(|def_id| self.tcx.parent(def_id));

A similar mechanism should probably also be used for consts.

Activity

added
A-const-genericsArea: const generics (parameters and arguments)
A-diagnosticsArea: Messages for errors, warnings, and lints
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
and removed on May 18, 2020
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on May 18, 2020
varkor

varkor commented on Sep 13, 2020

@varkor
Member

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 min_const_generics.

lcnr

lcnr commented on Sep 13, 2020

@lcnr
ContributorAuthor

yeah, will look a bit into some interesting test cases here in the near future hopefully

lcnr

lcnr commented on Sep 14, 2020

@lcnr
ContributorAuthor

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

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

for baz<T> we instead get

error[E0282]: type annotations needed
  --> src/main.rs:17:33
   |
17 |     Foo.bar().bar().bar().bar().baz();
   |                                 ^^^ cannot infer type for type parameter `T` declared on the associated function `baz`

btw I am not actually sure if this issue makes sense, we already have the origin of const infer values stored, so
I don't actually think we need to add a new storage here 😆 we "just" have to use the current one

pickfire

pickfire commented on Sep 14, 2020

@pickfire
Contributor

Is there any suggestion for user on how to fix this?

8 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.F-const_generics`#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonas-schievink@varkor@pickfire@lcnr

        Issue actions

          Add a storage for the origin of const inference variables. · Issue #72328 · rust-lang/rust