Skip to content

Commit 077fae9

Browse files
committed
Add note about wrapping in braces
Previously it was not clear why this errored or if it was even supported, as there was no diagnostic that suggested wrapping it in braces. Thus, add a simple diagnostic that suggests wrapping enum variants in braces.
1 parent 2c7536e commit 077fae9

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -1199,17 +1199,26 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11991199
(_, _) => {
12001200
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
12011201
let expected = def_kind.descr(assoc_item_def_id);
1202-
let reported = tcx
1203-
.sess
1204-
.struct_span_err(
1202+
let mut err = tcx.sess.struct_span_err(
1203+
binding.span,
1204+
&format!("expected {expected} bound, found {got}"),
1205+
);
1206+
err.span_note(
1207+
tcx.def_span(assoc_item_def_id),
1208+
&format!("{expected} defined here"),
1209+
);
1210+
1211+
if let hir::def::DefKind::AssocConst = def_kind
1212+
&& let Some(t) = term.ty() && (t.is_enum() || t.references_error())
1213+
&& tcx.features().associated_const_equality {
1214+
err.span_suggestion(
12051215
binding.span,
1206-
&format!("expected {expected} bound, found {got}"),
1207-
)
1208-
.span_note(
1209-
tcx.def_span(assoc_item_def_id),
1210-
&format!("{expected} defined here"),
1211-
)
1212-
.emit();
1216+
"if equating a const, try wrapping with braces",
1217+
format!("{} = {{ const }}", binding.item_name),
1218+
Applicability::HasPlaceholders,
1219+
);
1220+
}
1221+
let reported = err.emit();
12131222
term = match def_kind {
12141223
hir::def::DefKind::AssocTy => {
12151224
tcx.ty_error_with_guaranteed(reported).into()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(associated_const_equality)]
2+
3+
pub enum Mode {
4+
Cool,
5+
}
6+
7+
pub trait Parse {
8+
const MODE: Mode;
9+
}
10+
11+
pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
12+
//~^ ERROR expected associated constant bound
13+
//~| ERROR expected type
14+
15+
fn no_help() -> Mode::Cool {}
16+
//~^ ERROR expected type, found variant
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0573]: expected type, found variant `Mode::Cool`
2+
--> $DIR/assoc_const_eq_diagnostic.rs:11:35
3+
|
4+
LL | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
5+
| ^^^^^^^^^^
6+
| |
7+
| not a type
8+
| help: try using the variant's enum: `Mode`
9+
10+
error[E0573]: expected type, found variant `Mode::Cool`
11+
--> $DIR/assoc_const_eq_diagnostic.rs:15:17
12+
|
13+
LL | fn no_help() -> Mode::Cool {}
14+
| ^^^^^^^^^^
15+
| |
16+
| not a type
17+
| help: try using the variant's enum: `Mode`
18+
19+
error: expected associated constant bound, found type
20+
--> $DIR/assoc_const_eq_diagnostic.rs:11:28
21+
|
22+
LL | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
23+
| ^^^^^^^^^^^^^^^^^ help: if equating a const, try wrapping with braces: `MODE = { const }`
24+
|
25+
note: associated constant defined here
26+
--> $DIR/assoc_const_eq_diagnostic.rs:8:5
27+
|
28+
LL | const MODE: Mode;
29+
| ^^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0573`.

0 commit comments

Comments
 (0)