Skip to content

Add help diag. for const = Enum missing braces around Enum #106283

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

Merged
merged 1 commit into from
Jan 7, 2023
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
29 changes: 19 additions & 10 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,17 +1199,26 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
(_, _) => {
let got = if let Some(_) = term.ty() { "type" } else { "constant" };
let expected = def_kind.descr(assoc_item_def_id);
let reported = tcx
.sess
.struct_span_err(
let mut err = tcx.sess.struct_span_err(
binding.span,
&format!("expected {expected} bound, found {got}"),
);
err.span_note(
tcx.def_span(assoc_item_def_id),
&format!("{expected} defined here"),
);

if let hir::def::DefKind::AssocConst = def_kind
&& let Some(t) = term.ty() && (t.is_enum() || t.references_error())
&& tcx.features().associated_const_equality {
err.span_suggestion(
binding.span,
&format!("expected {expected} bound, found {got}"),
)
.span_note(
tcx.def_span(assoc_item_def_id),
&format!("{expected} defined here"),
)
.emit();
"if equating a const, try wrapping with braces",
format!("{} = {{ const }}", binding.item_name),
Applicability::HasPlaceholders,
);
}
let reported = err.emit();
term = match def_kind {
hir::def::DefKind::AssocTy => {
tcx.ty_error_with_guaranteed(reported).into()
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/const-generics/assoc_const_eq_diagnostic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(associated_const_equality)]

pub enum Mode {
Cool,
}

pub trait Parse {
const MODE: Mode;
}

pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
//~^ ERROR expected associated constant bound
//~| ERROR expected type

fn no_help() -> Mode::Cool {}
//~^ ERROR expected type, found variant

fn main() {}
33 changes: 33 additions & 0 deletions src/test/ui/const-generics/assoc_const_eq_diagnostic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0573]: expected type, found variant `Mode::Cool`
--> $DIR/assoc_const_eq_diagnostic.rs:11:35
|
LL | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
| ^^^^^^^^^^
| |
| not a type
| help: try using the variant's enum: `Mode`

error[E0573]: expected type, found variant `Mode::Cool`
--> $DIR/assoc_const_eq_diagnostic.rs:15:17
|
LL | fn no_help() -> Mode::Cool {}
| ^^^^^^^^^^
| |
| not a type
| help: try using the variant's enum: `Mode`

error: expected associated constant bound, found type
--> $DIR/assoc_const_eq_diagnostic.rs:11:28
|
LL | pub trait CoolStuff: Parse<MODE = Mode::Cool> {}
| ^^^^^^^^^^^^^^^^^ help: if equating a const, try wrapping with braces: `MODE = { const }`
|
note: associated constant defined here
--> $DIR/assoc_const_eq_diagnostic.rs:8:5
|
LL | const MODE: Mode;
| ^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

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