Skip to content

Commit 1083833

Browse files
authored
Rollup merge of #76401 - JulianKnodt:i68366, r=lcnr
Add help note to unconstrained const parameter Resolves #68366, since it is currently intended behaviour. If demonstrating `T -> U` is injective, there should be an additional word that it is not **yet** supported. r? @lcnr
2 parents d45faff + ee55c1f commit 1083833

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

compiler/rustc_typeck/src/impl_wf_check.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn enforce_impl_params_are_constrained(
187187
}
188188

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

209209
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: &str) {
210-
struct_span_err!(
210+
let mut err = struct_span_err!(
211211
tcx.sess,
212212
span,
213213
E0207,
214214
"the {} parameter `{}` is not constrained by the \
215215
impl trait, self type, or predicates",
216216
kind,
217217
name
218-
)
219-
.span_label(span, format!("unconstrained {} parameter", kind))
220-
.emit();
218+
);
219+
err.span_label(span, format!("unconstrained {} parameter", kind));
220+
if kind == "const" {
221+
err.note(
222+
"expressions using a const parameter must map each value to a distinct output value",
223+
);
224+
err.note(
225+
"proving the result of expressions other than the parameter are unique is not supported",
226+
);
227+
}
228+
err.emit();
221229
}
222230

223231
/// Enforce that we do not have two items in an impl with the same name.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Checks that const expressions have a useful note explaining why they can't be evaluated.
2+
// The note should relate to the fact that it cannot be shown forall N that it maps 1-1 to a new
3+
// type.
4+
5+
#![feature(const_generics)]
6+
#![allow(incomplete_features)]
7+
8+
struct Collatz<const N: Option<usize>>;
9+
10+
impl <const N: usize> Collatz<{Some(N)}> {}
11+
//~^ ERROR the const parameter
12+
13+
struct Foo;
14+
15+
impl<const N: usize> Foo {}
16+
//~^ ERROR the const parameter
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/issue-68366.rs:10:13
3+
|
4+
LL | impl <const N: usize> Collatz<{Some(N)}> {}
5+
| ^ unconstrained const parameter
6+
|
7+
= note: expressions using a const parameter must map each value to a distinct output value
8+
= note: proving the result of expressions other than the parameter are unique is not supported
9+
10+
error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
11+
--> $DIR/issue-68366.rs:15:12
12+
|
13+
LL | impl<const N: usize> Foo {}
14+
| ^ unconstrained const parameter
15+
|
16+
= note: expressions using a const parameter must map each value to a distinct output value
17+
= note: proving the result of expressions other than the parameter are unique is not supported
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)