mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Rollup merge of #127891 - estebank:enum-type-sugg, r=estebank
Tweak suggestions when using incorrect type of enum literal More accurate suggestions when writing wrong style of enum variant literal: ``` error[E0533]: expected value, found struct variant `E::Empty3` --> $DIR/empty-struct-braces-expr.rs:18:14 | LL | let e3 = E::Empty3; | ^^^^^^^^^ not a value | help: you might have meant to create a new value of the struct | LL | let e3 = E::Empty3 {}; | ++ ``` ``` error[E0533]: expected value, found struct variant `E::V` --> $DIR/struct-literal-variant-in-if.rs:10:13 | LL | if x == E::V { field } {} | ^^^^ not a value | help: you might have meant to create a new value of the struct | LL | if x == (E::V { field }) {} | + + ``` ``` error[E0618]: expected function, found enum variant `Enum::Unit` --> $DIR/suggestion-highlights.rs:15:5 | LL | Unit, | ---- enum variant `Enum::Unit` defined here ... LL | Enum::Unit(); | ^^^^^^^^^^-- | | | call expression requires function | help: `Enum::Unit` is a unit enum variant, and does not take parentheses to be constructed | LL - Enum::Unit(); LL + Enum::Unit; | ``` ``` error[E0599]: no variant or associated item named `tuple` found for enum `Enum` in the current scope --> $DIR/suggestion-highlights.rs:36:11 | LL | enum Enum { | --------- variant or associated item `tuple` not found for this enum ... LL | Enum::tuple; | ^^^^^ variant or associated item not found in `Enum` | help: there is a variant with a similar name | LL | Enum::Tuple(/* i32 */); | ~~~~~~~~~~~~~~~~; | ``` Tweak "field not found" suggestion when giving struct literal for tuple struct type: ``` error[E0560]: struct `S` has no field named `x` --> $DIR/nested-non-tuple-tuple-struct.rs:8:19 | LL | pub struct S(f32, f32); | - `S` defined here ... LL | let _x = (S { x: 1.0, y: 2.0 }, S { x: 3.0, y: 4.0 }); | ^ field does not exist | help: `S` is a tuple struct, use the appropriate syntax | LL | let _x = (S(/* f32 */, /* f32 */), S { x: 3.0, y: 4.0 }); | ~~~~~~~~~~~~~~~~~~~~~~~
This commit is contained in:
commit
fc6e34f38f
@ -1087,7 +1087,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||
);
|
||||
|
||||
let adt_def = qself_ty.ty_adt_def().expect("enum is not an ADT");
|
||||
if let Some(suggested_name) = find_best_match_for_name(
|
||||
if let Some(variant_name) = find_best_match_for_name(
|
||||
&adt_def
|
||||
.variants()
|
||||
.iter()
|
||||
@ -1095,12 +1095,66 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||
.collect::<Vec<Symbol>>(),
|
||||
assoc_ident.name,
|
||||
None,
|
||||
) {
|
||||
err.span_suggestion(
|
||||
assoc_ident.span,
|
||||
) && let Some(variant) =
|
||||
adt_def.variants().iter().find(|s| s.name == variant_name)
|
||||
{
|
||||
let mut suggestion = vec![(assoc_ident.span, variant_name.to_string())];
|
||||
if let hir::Node::Stmt(hir::Stmt {
|
||||
kind: hir::StmtKind::Semi(ref expr),
|
||||
..
|
||||
})
|
||||
| hir::Node::Expr(ref expr) = tcx.parent_hir_node(hir_ref_id)
|
||||
&& let hir::ExprKind::Struct(..) = expr.kind
|
||||
{
|
||||
match variant.ctor {
|
||||
None => {
|
||||
// struct
|
||||
suggestion = vec![(
|
||||
assoc_ident.span.with_hi(expr.span.hi()),
|
||||
if variant.fields.is_empty() {
|
||||
format!("{variant_name} {{}}")
|
||||
} else {
|
||||
format!(
|
||||
"{variant_name} {{ {} }}",
|
||||
variant
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| format!("{}: /* value */", f.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
},
|
||||
)];
|
||||
}
|
||||
Some((hir::def::CtorKind::Fn, def_id)) => {
|
||||
// tuple
|
||||
let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
|
||||
let inputs = fn_sig.inputs().skip_binder();
|
||||
suggestion = vec![(
|
||||
assoc_ident.span.with_hi(expr.span.hi()),
|
||||
format!(
|
||||
"{variant_name}({})",
|
||||
inputs
|
||||
.iter()
|
||||
.map(|i| format!("/* {i} */"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
)];
|
||||
}
|
||||
Some((hir::def::CtorKind::Const, _)) => {
|
||||
// unit
|
||||
suggestion = vec![(
|
||||
assoc_ident.span.with_hi(expr.span.hi()),
|
||||
variant_name.to_string(),
|
||||
)];
|
||||
}
|
||||
}
|
||||
}
|
||||
err.multipart_suggestion_verbose(
|
||||
"there is a variant with a similar name",
|
||||
suggested_name,
|
||||
Applicability::MaybeIncorrect,
|
||||
suggestion,
|
||||
Applicability::HasPlaceholders,
|
||||
);
|
||||
} else {
|
||||
err.span_label(
|
||||
|
@ -519,7 +519,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
Ty::new_error(tcx, e)
|
||||
}
|
||||
Res::Def(DefKind::Variant, _) => {
|
||||
let e = report_unexpected_variant_res(tcx, res, qpath, expr.span, E0533, "value");
|
||||
let e = report_unexpected_variant_res(
|
||||
tcx,
|
||||
res,
|
||||
Some(expr),
|
||||
qpath,
|
||||
expr.span,
|
||||
E0533,
|
||||
"value",
|
||||
);
|
||||
Ty::new_error(tcx, e)
|
||||
}
|
||||
_ => {
|
||||
@ -2210,8 +2218,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
);
|
||||
|
||||
let variant_ident_span = self.tcx.def_ident_span(variant.def_id).unwrap();
|
||||
match variant.ctor_kind() {
|
||||
Some(CtorKind::Fn) => match ty.kind() {
|
||||
match variant.ctor {
|
||||
Some((CtorKind::Fn, def_id)) => match ty.kind() {
|
||||
ty::Adt(adt, ..) if adt.is_enum() => {
|
||||
err.span_label(
|
||||
variant_ident_span,
|
||||
@ -2222,28 +2230,44 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
),
|
||||
);
|
||||
err.span_label(field.ident.span, "field does not exist");
|
||||
let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
|
||||
let inputs = fn_sig.inputs().skip_binder();
|
||||
let fields = format!(
|
||||
"({})",
|
||||
inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
|
||||
);
|
||||
let (replace_span, sugg) = match expr.kind {
|
||||
hir::ExprKind::Struct(qpath, ..) => {
|
||||
(qpath.span().shrink_to_hi().with_hi(expr.span.hi()), fields)
|
||||
}
|
||||
_ => {
|
||||
(expr.span, format!("{ty}::{variant}{fields}", variant = variant.name))
|
||||
}
|
||||
};
|
||||
err.span_suggestion_verbose(
|
||||
expr.span,
|
||||
replace_span,
|
||||
format!(
|
||||
"`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
|
||||
adt = ty,
|
||||
variant = variant.name,
|
||||
),
|
||||
format!(
|
||||
"{adt}::{variant}(/* fields */)",
|
||||
adt = ty,
|
||||
variant = variant.name,
|
||||
),
|
||||
sugg,
|
||||
Applicability::HasPlaceholders,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
err.span_label(variant_ident_span, format!("`{ty}` defined here"));
|
||||
err.span_label(field.ident.span, "field does not exist");
|
||||
let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
|
||||
let inputs = fn_sig.inputs().skip_binder();
|
||||
let fields = format!(
|
||||
"({})",
|
||||
inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
|
||||
);
|
||||
err.span_suggestion_verbose(
|
||||
expr.span,
|
||||
format!("`{ty}` is a tuple {kind_name}, use the appropriate syntax",),
|
||||
format!("{ty}(/* fields */)"),
|
||||
format!("{ty}{fields}"),
|
||||
Applicability::HasPlaceholders,
|
||||
);
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ use crate::expectation::Expectation;
|
||||
use crate::fn_ctxt::LoweredTy;
|
||||
use crate::gather_locals::GatherLocalsVisitor;
|
||||
use rustc_data_structures::unord::UnordSet;
|
||||
use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed};
|
||||
use rustc_errors::{codes::*, struct_span_code_err, Applicability, ErrorGuaranteed};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{DefKind, Res};
|
||||
use rustc_hir::intravisit::Visitor;
|
||||
@ -346,6 +346,7 @@ impl<'tcx> EnclosingBreakables<'tcx> {
|
||||
fn report_unexpected_variant_res(
|
||||
tcx: TyCtxt<'_>,
|
||||
res: Res,
|
||||
expr: Option<&hir::Expr<'_>>,
|
||||
qpath: &hir::QPath<'_>,
|
||||
span: Span,
|
||||
err_code: ErrCode,
|
||||
@ -356,7 +357,7 @@ fn report_unexpected_variant_res(
|
||||
_ => res.descr(),
|
||||
};
|
||||
let path_str = rustc_hir_pretty::qpath_to_string(&tcx, qpath);
|
||||
let err = tcx
|
||||
let mut err = tcx
|
||||
.dcx()
|
||||
.struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
|
||||
.with_code(err_code);
|
||||
@ -366,6 +367,61 @@ fn report_unexpected_variant_res(
|
||||
err.with_span_label(span, "`fn` calls are not allowed in patterns")
|
||||
.with_help(format!("for more information, visit {patterns_url}"))
|
||||
}
|
||||
Res::Def(DefKind::Variant, _) if let Some(expr) = expr => {
|
||||
err.span_label(span, format!("not a {expected}"));
|
||||
let variant = tcx.expect_variant_res(res);
|
||||
let sugg = if variant.fields.is_empty() {
|
||||
" {}".to_string()
|
||||
} else {
|
||||
format!(
|
||||
" {{ {} }}",
|
||||
variant
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| format!("{}: /* value */", f.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
};
|
||||
let descr = "you might have meant to create a new value of the struct";
|
||||
let mut suggestion = vec![];
|
||||
match tcx.parent_hir_node(expr.hir_id) {
|
||||
hir::Node::Expr(hir::Expr {
|
||||
kind: hir::ExprKind::Call(..),
|
||||
span: call_span,
|
||||
..
|
||||
}) => {
|
||||
suggestion.push((span.shrink_to_hi().with_hi(call_span.hi()), sugg));
|
||||
}
|
||||
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(..), hir_id, .. }) => {
|
||||
suggestion.push((expr.span.shrink_to_lo(), "(".to_string()));
|
||||
if let hir::Node::Expr(drop_temps) = tcx.parent_hir_node(*hir_id)
|
||||
&& let hir::ExprKind::DropTemps(_) = drop_temps.kind
|
||||
&& let hir::Node::Expr(parent) = tcx.parent_hir_node(drop_temps.hir_id)
|
||||
&& let hir::ExprKind::If(condition, block, None) = parent.kind
|
||||
&& condition.hir_id == drop_temps.hir_id
|
||||
&& let hir::ExprKind::Block(block, _) = block.kind
|
||||
&& block.stmts.is_empty()
|
||||
&& let Some(expr) = block.expr
|
||||
&& let hir::ExprKind::Path(..) = expr.kind
|
||||
{
|
||||
// Special case: you can incorrectly write an equality condition:
|
||||
// if foo == Struct { field } { /* if body */ }
|
||||
// which should have been written
|
||||
// if foo == (Struct { field }) { /* if body */ }
|
||||
suggestion.push((block.span.shrink_to_hi(), ")".to_string()));
|
||||
} else {
|
||||
suggestion.push((span.shrink_to_hi().with_hi(expr.span.hi()), sugg));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
suggestion.push((span.shrink_to_hi(), sugg));
|
||||
}
|
||||
}
|
||||
|
||||
err.multipart_suggestion_verbose(descr, suggestion, Applicability::MaybeIncorrect);
|
||||
err
|
||||
}
|
||||
_ => err.with_span_label(span, format!("not a {expected}")),
|
||||
}
|
||||
.emit()
|
||||
|
@ -1596,16 +1596,127 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
// that had unsatisfied trait bounds
|
||||
if unsatisfied_predicates.is_empty() && rcvr_ty.is_enum() {
|
||||
let adt_def = rcvr_ty.ty_adt_def().expect("enum is not an ADT");
|
||||
if let Some(suggestion) = edit_distance::find_best_match_for_name(
|
||||
if let Some(var_name) = edit_distance::find_best_match_for_name(
|
||||
&adt_def.variants().iter().map(|s| s.name).collect::<Vec<_>>(),
|
||||
item_name.name,
|
||||
None,
|
||||
) {
|
||||
err.span_suggestion(
|
||||
span,
|
||||
) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == var_name)
|
||||
{
|
||||
let mut suggestion = vec![(span, var_name.to_string())];
|
||||
if let SelfSource::QPath(ty) = source
|
||||
&& let hir::Node::Expr(ref path_expr) = self.tcx.parent_hir_node(ty.hir_id)
|
||||
&& let hir::ExprKind::Path(_) = path_expr.kind
|
||||
&& let hir::Node::Stmt(hir::Stmt {
|
||||
kind: hir::StmtKind::Semi(ref parent), ..
|
||||
})
|
||||
| hir::Node::Expr(ref parent) = self.tcx.parent_hir_node(path_expr.hir_id)
|
||||
{
|
||||
let replacement_span =
|
||||
if let hir::ExprKind::Call(..) | hir::ExprKind::Struct(..) = parent.kind {
|
||||
// We want to replace the parts that need to go, like `()` and `{}`.
|
||||
span.with_hi(parent.span.hi())
|
||||
} else {
|
||||
span
|
||||
};
|
||||
match (variant.ctor, parent.kind) {
|
||||
(None, hir::ExprKind::Struct(..)) => {
|
||||
// We want a struct and we have a struct. We won't suggest changing
|
||||
// the fields (at least for now).
|
||||
suggestion = vec![(span, var_name.to_string())];
|
||||
}
|
||||
(None, _) => {
|
||||
// struct
|
||||
suggestion = vec![(
|
||||
replacement_span,
|
||||
if variant.fields.is_empty() {
|
||||
format!("{var_name} {{}}")
|
||||
} else {
|
||||
format!(
|
||||
"{var_name} {{ {} }}",
|
||||
variant
|
||||
.fields
|
||||
.iter()
|
||||
.map(|f| format!("{}: /* value */", f.name))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
)
|
||||
},
|
||||
)];
|
||||
}
|
||||
(Some((hir::def::CtorKind::Const, _)), _) => {
|
||||
// unit, remove the `()`.
|
||||
suggestion = vec![(replacement_span, var_name.to_string())];
|
||||
}
|
||||
(
|
||||
Some((hir::def::CtorKind::Fn, def_id)),
|
||||
hir::ExprKind::Call(rcvr, args),
|
||||
) => {
|
||||
let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
|
||||
let inputs = fn_sig.inputs().skip_binder();
|
||||
// FIXME: reuse the logic for "change args" suggestion to account for types
|
||||
// involved and detect things like substitution.
|
||||
match (inputs, args) {
|
||||
(inputs, []) => {
|
||||
// Add arguments.
|
||||
suggestion.push((
|
||||
rcvr.span.shrink_to_hi().with_hi(parent.span.hi()),
|
||||
format!(
|
||||
"({})",
|
||||
inputs
|
||||
.iter()
|
||||
.map(|i| format!("/* {i} */"))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
));
|
||||
}
|
||||
(_, [arg]) if inputs.len() != args.len() => {
|
||||
// Replace arguments.
|
||||
suggestion.push((
|
||||
arg.span,
|
||||
inputs
|
||||
.iter()
|
||||
.map(|i| format!("/* {i} */"))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", "),
|
||||
));
|
||||
}
|
||||
(_, [arg_start, .., arg_end]) if inputs.len() != args.len() => {
|
||||
// Replace arguments.
|
||||
suggestion.push((
|
||||
arg_start.span.to(arg_end.span),
|
||||
inputs
|
||||
.iter()
|
||||
.map(|i| format!("/* {i} */"))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", "),
|
||||
));
|
||||
}
|
||||
// Argument count is the same, keep as is.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
(Some((hir::def::CtorKind::Fn, def_id)), _) => {
|
||||
let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity();
|
||||
let inputs = fn_sig.inputs().skip_binder();
|
||||
suggestion = vec![(
|
||||
replacement_span,
|
||||
format!(
|
||||
"{var_name}({})",
|
||||
inputs
|
||||
.iter()
|
||||
.map(|i| format!("/* {i} */"))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
)];
|
||||
}
|
||||
}
|
||||
}
|
||||
err.multipart_suggestion_verbose(
|
||||
"there is a variant with a similar name",
|
||||
suggestion,
|
||||
Applicability::MaybeIncorrect,
|
||||
Applicability::HasPlaceholders,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1023,7 +1023,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
}
|
||||
Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => {
|
||||
let expected = "unit struct, unit variant or constant";
|
||||
let e = report_unexpected_variant_res(tcx, res, qpath, pat.span, E0533, expected);
|
||||
let e =
|
||||
report_unexpected_variant_res(tcx, res, None, qpath, pat.span, E0533, expected);
|
||||
return Ty::new_error(tcx, e);
|
||||
}
|
||||
Res::SelfCtor(def_id) => {
|
||||
@ -1036,6 +1037,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
let e = report_unexpected_variant_res(
|
||||
tcx,
|
||||
res,
|
||||
None,
|
||||
qpath,
|
||||
pat.span,
|
||||
E0533,
|
||||
@ -1189,7 +1191,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
};
|
||||
let report_unexpected_res = |res: Res| {
|
||||
let expected = "tuple struct or tuple variant";
|
||||
let e = report_unexpected_variant_res(tcx, res, qpath, pat.span, E0164, expected);
|
||||
let e = report_unexpected_variant_res(tcx, res, None, qpath, pat.span, E0164, expected);
|
||||
on_error(e);
|
||||
e
|
||||
};
|
||||
|
@ -71,12 +71,22 @@ error[E0533]: expected value, found struct variant `E::Empty3`
|
||||
|
|
||||
LL | let e3 = E::Empty3;
|
||||
| ^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let e3 = E::Empty3 {};
|
||||
| ++
|
||||
|
||||
error[E0533]: expected value, found struct variant `E::Empty3`
|
||||
--> $DIR/empty-struct-braces-expr.rs:19:14
|
||||
|
|
||||
LL | let e3 = E::Empty3();
|
||||
| ^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let e3 = E::Empty3 {};
|
||||
| ~~
|
||||
|
||||
error[E0423]: expected function, tuple struct or tuple variant, found struct `XEmpty1`
|
||||
--> $DIR/empty-struct-braces-expr.rs:23:15
|
||||
@ -104,25 +114,34 @@ error[E0599]: no variant or associated item named `Empty3` found for enum `empty
|
||||
--> $DIR/empty-struct-braces-expr.rs:25:19
|
||||
|
|
||||
LL | let xe3 = XE::Empty3;
|
||||
| ^^^^^^
|
||||
| |
|
||||
| variant or associated item not found in `XE`
|
||||
| help: there is a variant with a similar name: `XEmpty3`
|
||||
| ^^^^^^ variant or associated item not found in `XE`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | let xe3 = XE::XEmpty3;
|
||||
| ~~~~~~~
|
||||
|
||||
error[E0599]: no variant or associated item named `Empty3` found for enum `empty_struct::XE` in the current scope
|
||||
--> $DIR/empty-struct-braces-expr.rs:26:19
|
||||
|
|
||||
LL | let xe3 = XE::Empty3();
|
||||
| ^^^^^^
|
||||
| |
|
||||
| variant or associated item not found in `XE`
|
||||
| help: there is a variant with a similar name: `XEmpty3`
|
||||
| ^^^^^^ variant or associated item not found in `XE`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | let xe3 = XE::XEmpty3 {};
|
||||
| ~~~~~~~~~~
|
||||
|
||||
error[E0599]: no variant named `Empty1` found for enum `empty_struct::XE`
|
||||
--> $DIR/empty-struct-braces-expr.rs:28:9
|
||||
|
|
||||
LL | XE::Empty1 {};
|
||||
| ^^^^^^ help: there is a variant with a similar name: `XEmpty3`
|
||||
| ^^^^^^
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | XE::XEmpty3 {};
|
||||
| ~~~~~~~~~~
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
|
@ -3,6 +3,11 @@ error[E0533]: expected value, found struct variant `Struct<0>::Variant`
|
||||
|
|
||||
LL | let x = Struct::<0>::Variant;
|
||||
| ^^^^^^^^^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let x = Struct::<0>::Variant { x: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -5,10 +5,12 @@ LL | enum Delicious {
|
||||
| -------------- variant or associated item `PIE` not found for this enum
|
||||
...
|
||||
LL | ApplePie = Delicious::Apple as isize | Delicious::PIE as isize,
|
||||
| ^^^
|
||||
| |
|
||||
| variant or associated item not found in `Delicious`
|
||||
| help: there is a variant with a similar name: `Pie`
|
||||
| ^^^ variant or associated item not found in `Delicious`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | ApplePie = Delicious::Apple as isize | Delicious::Pie as isize,
|
||||
| ~~~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -4,10 +4,12 @@ error[E0599]: no variant or associated item named `A` found for enum `SomeEnum`
|
||||
LL | pub enum SomeEnum {
|
||||
| ----------------- variant or associated item `A` not found for this enum
|
||||
LL | B = SomeEnum::A,
|
||||
| ^
|
||||
| |
|
||||
| variant or associated item not found in `SomeEnum`
|
||||
| help: there is a variant with a similar name: `B`
|
||||
| ^ variant or associated item not found in `SomeEnum`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | B = SomeEnum::B,
|
||||
| ~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -5,10 +5,12 @@ LL | enum Foo {
|
||||
| -------- variant or associated item `Baz` not found for this enum
|
||||
...
|
||||
LL | Foo::Baz(..) => (),
|
||||
| ^^^
|
||||
| |
|
||||
| variant or associated item not found in `Foo`
|
||||
| help: there is a variant with a similar name: `Bar`
|
||||
| ^^^ variant or associated item not found in `Foo`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | Foo::Bar(..) => (),
|
||||
| ~~~
|
||||
|
||||
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
|
||||
--> $DIR/issue-28971.rs:15:5
|
||||
|
@ -5,7 +5,12 @@ LL | enum S {
|
||||
| ------ variant `B` not found here
|
||||
...
|
||||
LL | S::B {} => {},
|
||||
| ^ help: there is a variant with a similar name: `A`
|
||||
| ^
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | S::A {} => {},
|
||||
| ~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -9,8 +9,8 @@ LL | let z = NonCopyable{ p: () };
|
||||
|
|
||||
help: `NonCopyable` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let z = NonCopyable(/* fields */);
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
LL | let z = NonCopyable(/* () */);
|
||||
| ~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -9,8 +9,8 @@ LL | Enum::V1 { x }
|
||||
|
|
||||
help: `Enum::V1` is a tuple variant, use the appropriate syntax
|
||||
|
|
||||
LL | Enum::V1(/* fields */)
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
LL | Enum::V1(/* i32 */)
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -9,8 +9,8 @@ LL | let s = S{0b1: 10, 0: 11};
|
||||
|
|
||||
help: `S` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let s = S(/* fields */);
|
||||
| ~~~~~~~~~~~~~~~
|
||||
LL | let s = S(/* u8 */, /* u16 */);
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0026]: struct `S` does not have a field named `0x1`
|
||||
--> $DIR/numeric-fields.rs:7:17
|
||||
|
@ -47,6 +47,11 @@ error[E0533]: expected value, found struct variant `E::V`
|
||||
|
|
||||
LL | if x == E::V { field } {}
|
||||
| ^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | if x == (E::V { field }) {}
|
||||
| + +
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/struct-literal-variant-in-if.rs:10:20
|
||||
|
@ -3,6 +3,11 @@ error[E0533]: expected value, found struct variant `Foo::Variant`
|
||||
|
|
||||
LL | let f = Foo::Variant(42);
|
||||
| ^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let f = Foo::Variant { x: /* value */ };
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
@ -3,12 +3,22 @@ error[E0533]: expected value, found struct variant `Homura::Madoka`
|
||||
|
|
||||
LL | let homura = Homura::Madoka;
|
||||
| ^^^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let homura = Homura::Madoka { age: /* value */ };
|
||||
| ++++++++++++++++++++
|
||||
|
||||
error[E0533]: expected value, found struct variant `issue_19452_aux::Homura::Madoka`
|
||||
--> $DIR/issue-19452.rs:13:18
|
||||
|
|
||||
LL | let homura = issue_19452_aux::Homura::Madoka;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let homura = issue_19452_aux::Homura::Madoka { age: /* value */ };
|
||||
| ++++++++++++++++++++
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
@ -295,6 +295,11 @@ error[E0533]: expected value, found struct variant `Z::Struct`
|
||||
|
|
||||
LL | let _: Z = Z::Struct;
|
||||
| ^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let _: Z = Z::Struct { s: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error[E0618]: expected function, found enum variant `Z::Unit`
|
||||
--> $DIR/privacy-enum-ctor.rs:31:17
|
||||
@ -336,6 +341,11 @@ error[E0533]: expected value, found struct variant `m::E::Struct`
|
||||
|
|
||||
LL | let _: E = m::E::Struct;
|
||||
| ^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let _: E = m::E::Struct { s: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error[E0618]: expected function, found enum variant `m::E::Unit`
|
||||
--> $DIR/privacy-enum-ctor.rs:47:16
|
||||
@ -377,6 +387,11 @@ error[E0533]: expected value, found struct variant `E::Struct`
|
||||
|
|
||||
LL | let _: E = E::Struct;
|
||||
| ^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let _: E = E::Struct { s: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error[E0618]: expected function, found enum variant `E::Unit`
|
||||
--> $DIR/privacy-enum-ctor.rs:55:16
|
||||
@ -400,6 +415,11 @@ error[E0533]: expected value, found struct variant `m::n::Z::Struct`
|
||||
|
|
||||
LL | let _: Z = m::n::Z::Struct;
|
||||
| ^^^^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let _: Z = m::n::Z::Struct { s: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error: aborting due to 23 previous errors
|
||||
|
||||
|
@ -129,6 +129,11 @@ error[E0533]: expected value, found struct variant `E::B`
|
||||
|
|
||||
LL | let _: E = E::B;
|
||||
| ^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | let _: E = E::B { a: /* value */ };
|
||||
| ++++++++++++++++++
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/fn-or-tuple-struct-without-args.rs:37:20
|
||||
|
55
tests/ui/suggestions/incorrect-variant-literal.rs
Normal file
55
tests/ui/suggestions/incorrect-variant-literal.rs
Normal file
@ -0,0 +1,55 @@
|
||||
//@ only-linux
|
||||
//@ compile-flags: --error-format=human --color=always
|
||||
|
||||
enum Enum {
|
||||
Unit,
|
||||
Tuple(i32),
|
||||
Struct { x: i32 },
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Enum::Unit;
|
||||
Enum::Tuple;
|
||||
Enum::Struct;
|
||||
Enum::Unit();
|
||||
Enum::Tuple();
|
||||
Enum::Struct();
|
||||
Enum::Unit {};
|
||||
Enum::Tuple {};
|
||||
Enum::Struct {};
|
||||
Enum::Unit(0);
|
||||
Enum::Tuple(0);
|
||||
Enum::Struct(0);
|
||||
Enum::Unit { x: 0 };
|
||||
Enum::Tuple { x: 0 };
|
||||
Enum::Struct { x: 0 }; // ok
|
||||
Enum::Unit(0, 0);
|
||||
Enum::Tuple(0, 0);
|
||||
Enum::Struct(0, 0);
|
||||
Enum::Unit { x: 0, y: 0 };
|
||||
|
||||
Enum::Tuple { x: 0, y: 0 };
|
||||
|
||||
Enum::Struct { x: 0, y: 0 };
|
||||
Enum::unit;
|
||||
Enum::tuple;
|
||||
Enum::r#struct;
|
||||
Enum::unit();
|
||||
Enum::tuple();
|
||||
Enum::r#struct();
|
||||
Enum::unit {};
|
||||
Enum::tuple {};
|
||||
Enum::r#struct {};
|
||||
Enum::unit(0);
|
||||
Enum::tuple(0);
|
||||
Enum::r#struct(0);
|
||||
Enum::unit { x: 0 };
|
||||
Enum::tuple { x: 0 };
|
||||
Enum::r#struct { x: 0 };
|
||||
Enum::unit(0, 0);
|
||||
Enum::tuple(0, 0);
|
||||
Enum::r#struct(0, 0);
|
||||
Enum::unit { x: 0, y: 0 };
|
||||
Enum::tuple { x: 0, y: 0 };
|
||||
Enum::r#struct { x: 0, y: 0 };
|
||||
}
|
1028
tests/ui/suggestions/incorrect-variant-literal.svg
Normal file
1028
tests/ui/suggestions/incorrect-variant-literal.svg
Normal file
File diff suppressed because it is too large
Load Diff
After Width: | Height: | Size: 78 KiB |
@ -9,8 +9,8 @@ LL | let _x = (S { x: 1.0, y: 2.0 }, S { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `S` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let _x = (S(/* fields */), S { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~
|
||||
LL | let _x = (S(/* f32 */, /* f32 */), S { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0560]: struct `S` has no field named `y`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:8:27
|
||||
@ -23,8 +23,8 @@ LL | let _x = (S { x: 1.0, y: 2.0 }, S { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `S` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let _x = (S(/* fields */), S { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~
|
||||
LL | let _x = (S(/* f32 */, /* f32 */), S { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0560]: struct `S` has no field named `x`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:8:41
|
||||
@ -37,8 +37,8 @@ LL | let _x = (S { x: 1.0, y: 2.0 }, S { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `S` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let _x = (S { x: 1.0, y: 2.0 }, S(/* fields */));
|
||||
| ~~~~~~~~~~~~~~~
|
||||
LL | let _x = (S { x: 1.0, y: 2.0 }, S(/* f32 */, /* f32 */));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0560]: struct `S` has no field named `y`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:8:49
|
||||
@ -51,8 +51,8 @@ LL | let _x = (S { x: 1.0, y: 2.0 }, S { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `S` is a tuple struct, use the appropriate syntax
|
||||
|
|
||||
LL | let _x = (S { x: 1.0, y: 2.0 }, S(/* fields */));
|
||||
| ~~~~~~~~~~~~~~~
|
||||
LL | let _x = (S { x: 1.0, y: 2.0 }, S(/* f32 */, /* f32 */));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0559]: variant `E::V` has no field named `x`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:13:22
|
||||
@ -65,8 +65,8 @@ LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `E::V` is a tuple variant, use the appropriate syntax
|
||||
|
|
||||
LL | let _y = (E::V(/* fields */), E::V { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
LL | let _y = (E::V(/* f32 */, /* f32 */), E::V { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0559]: variant `E::V` has no field named `y`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:13:30
|
||||
@ -79,8 +79,8 @@ LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `E::V` is a tuple variant, use the appropriate syntax
|
||||
|
|
||||
LL | let _y = (E::V(/* fields */), E::V { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
LL | let _y = (E::V(/* f32 */, /* f32 */), E::V { x: 3.0, y: 4.0 });
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0559]: variant `E::V` has no field named `x`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:13:47
|
||||
@ -93,8 +93,8 @@ LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `E::V` is a tuple variant, use the appropriate syntax
|
||||
|
|
||||
LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V(/* fields */));
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V(/* f32 */, /* f32 */));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0559]: variant `E::V` has no field named `y`
|
||||
--> $DIR/nested-non-tuple-tuple-struct.rs:13:55
|
||||
@ -107,8 +107,8 @@ LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V { x: 3.0, y: 4.0 });
|
||||
|
|
||||
help: `E::V` is a tuple variant, use the appropriate syntax
|
||||
|
|
||||
LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V(/* fields */));
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
LL | let _y = (E::V { x: 1.0, y: 2.0 }, E::V(/* f32 */, /* f32 */));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
||||
|
@ -5,7 +5,12 @@ LL | enum Shape {
|
||||
| ---------- variant `Squareee` not found here
|
||||
...
|
||||
LL | println!("My shape is {:?}", Shape::Squareee { size: 5});
|
||||
| ^^^^^^^^ help: there is a variant with a similar name: `Square`
|
||||
| ^^^^^^^^
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | println!("My shape is {:?}", Shape::Square { size: 5});
|
||||
| ~~~~~~
|
||||
|
||||
error[E0599]: no variant named `Circl` found for enum `Shape`
|
||||
--> $DIR/suggest-variants.rs:13:41
|
||||
@ -14,7 +19,12 @@ LL | enum Shape {
|
||||
| ---------- variant `Circl` not found here
|
||||
...
|
||||
LL | println!("My shape is {:?}", Shape::Circl { size: 5});
|
||||
| ^^^^^ help: there is a variant with a similar name: `Circle`
|
||||
| ^^^^^
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | println!("My shape is {:?}", Shape::Circle { size: 5});
|
||||
| ~~~~~~
|
||||
|
||||
error[E0599]: no variant named `Rombus` found for enum `Shape`
|
||||
--> $DIR/suggest-variants.rs:14:41
|
||||
@ -32,10 +42,12 @@ LL | enum Shape {
|
||||
| ---------- variant or associated item `Squareee` not found for this enum
|
||||
...
|
||||
LL | Shape::Squareee;
|
||||
| ^^^^^^^^
|
||||
| |
|
||||
| variant or associated item not found in `Shape`
|
||||
| help: there is a variant with a similar name: `Square`
|
||||
| ^^^^^^^^ variant or associated item not found in `Shape`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | Shape::Square { size: /* value */ };
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0599]: no variant or associated item named `Circl` found for enum `Shape` in the current scope
|
||||
--> $DIR/suggest-variants.rs:16:12
|
||||
@ -44,10 +56,12 @@ LL | enum Shape {
|
||||
| ---------- variant or associated item `Circl` not found for this enum
|
||||
...
|
||||
LL | Shape::Circl;
|
||||
| ^^^^^
|
||||
| |
|
||||
| variant or associated item not found in `Shape`
|
||||
| help: there is a variant with a similar name: `Circle`
|
||||
| ^^^^^ variant or associated item not found in `Shape`
|
||||
|
|
||||
help: there is a variant with a similar name
|
||||
|
|
||||
LL | Shape::Circle { radius: /* value */ };
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0599]: no variant or associated item named `Rombus` found for enum `Shape` in the current scope
|
||||
--> $DIR/suggest-variants.rs:17:12
|
||||
|
@ -3,6 +3,11 @@ error[E0533]: expected value, found struct variant `Alias::Braced`
|
||||
|
|
||||
LL | Alias::Braced;
|
||||
| ^^^^^^^^^^^^^ not a value
|
||||
|
|
||||
help: you might have meant to create a new value of the struct
|
||||
|
|
||||
LL | Alias::Braced {};
|
||||
| ++
|
||||
|
||||
error[E0533]: expected unit struct, unit variant or constant, found struct variant `Alias::Braced`
|
||||
--> $DIR/incorrect-variant-form-through-alias-caught.rs:10:9
|
||||
|
Loading…
Reference in New Issue
Block a user