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

Add help note to unconstrained const parameter #76401

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions compiler/rustc_typeck/src/impl_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn enforce_impl_params_are_constrained(
}

// (*) This is a horrible concession to reality. I think it'd be
// better to just ban unconstrianed lifetimes outright, but in
// better to just ban unconstrained lifetimes outright, but in
// practice people do non-hygenic macros like:
//
// ```
Expand All @@ -207,17 +207,25 @@ fn enforce_impl_params_are_constrained(
}

fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: &str) {
struct_span_err!(
let mut err = struct_span_err!(
tcx.sess,
span,
E0207,
"the {} parameter `{}` is not constrained by the \
impl trait, self type, or predicates",
kind,
name
)
.span_label(span, format!("unconstrained {} parameter", kind))
.emit();
);
err.span_label(span, format!("unconstrained {} parameter", kind));
if kind == "const" {
err.note(
"expressions using a const parameter must map each value to a distinct output value",
);
err.note(
"proving the result of expressions other than the parameter are unique is not supported",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the meaning of this comment may not be entirely clear to someone unfamiliar with the constraints of const generics, but I'm not sure of better wording yet. I'll have a think about it.

);
}
err.emit();
}

/// Enforce that we do not have two items in an impl with the same name.
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/issues/issue-68366.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Checks that const expressions have a useful note explaining why they can't be evaluated.
// The note should relate to the fact that it cannot be shown forall N that it maps 1-1 to a new
// type.

#![feature(const_generics)]
#![allow(incomplete_features)]

struct Collatz<const N: Option<usize>>;

impl <const N: usize> Collatz<{Some(N)}> {}
//~^ ERROR the const parameter

struct Foo;

impl<const N: usize> Foo {}
//~^ ERROR the const parameter

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/const-generics/issues/issue-68366.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-68366.rs:10:13
|
LL | impl <const N: usize> Collatz<{Some(N)}> {}
| ^ unconstrained const parameter
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
--> $DIR/issue-68366.rs:15:12
|
LL | impl<const N: usize> Foo {}
| ^ unconstrained const parameter
|
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0207`.