1
Fork 0

Rollup merge of #99008 - obeis:issue-98974, r=compiler-errors

Adding suggestion for E0530

Closes #98974
This commit is contained in:
Dylan DPC 2022-07-09 11:28:06 +05:30 committed by GitHub
commit d75a5723db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 19 deletions

View file

@ -28,7 +28,7 @@ use rustc_span::{BytePos, Span};
use tracing::debug;
use crate::imports::{Import, ImportKind, ImportResolver};
use crate::late::Rib;
use crate::late::{PatternSource, Rib};
use crate::path_names_to_string;
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
@ -896,25 +896,40 @@ impl<'a> Resolver<'a> {
err
}
ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr,
shadowing_binding,
name,
participle,
article,
shadowed_binding_descr,
shadowed_binding,
shadowed_binding_span,
} => {
let shadowed_binding_descr = shadowed_binding.descr();
let mut err = struct_span_err!(
self.session,
span,
E0530,
"{}s cannot shadow {}s",
shadowing_binding_descr,
shadowing_binding.descr(),
shadowed_binding_descr,
);
err.span_label(
span,
format!("cannot be named the same as {} {}", article, shadowed_binding_descr),
);
match (shadowing_binding, shadowed_binding) {
(
PatternSource::Match,
Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
) => {
err.span_suggestion(
span,
"try specify the pattern arguments",
format!("{}(..)", name),
Applicability::Unspecified,
);
}
_ => (),
}
let msg =
format!("the {} `{}` is {} here", shadowed_binding_descr, name, participle);
err.span_label(shadowed_binding_span, msg);

View file

@ -50,7 +50,7 @@ struct BindingInfo {
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum PatternSource {
pub enum PatternSource {
Match,
Let,
For,
@ -64,7 +64,7 @@ enum IsRepeatExpr {
}
impl PatternSource {
fn descr(self) -> &'static str {
pub fn descr(self) -> &'static str {
match self {
PatternSource::Match => "match binding",
PatternSource::Let => "let binding",
@ -2845,11 +2845,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_error(
ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: pat_src.descr(),
shadowing_binding: pat_src,
name: ident.name,
participle: if binding.is_import() { "imported" } else { "defined" },
article: binding.res().article(),
shadowed_binding_descr: binding.res().descr(),
shadowed_binding: binding.res(),
shadowed_binding_span: binding.span,
},
);
@ -2861,11 +2861,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_error(
ident.span,
ResolutionError::BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: pat_src.descr(),
shadowing_binding: pat_src,
name: ident.name,
participle: "defined",
article: res.article(),
shadowed_binding_descr: res.descr(),
shadowed_binding: res,
shadowed_binding_span: self.r.opt_span(def_id).expect("const parameter defined outside of local crate"),
}
);

View file

@ -61,7 +61,7 @@ use tracing::debug;
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
use imports::{Import, ImportKind, ImportResolver, NameResolution};
use late::{HasGenericParams, PathSource};
use late::{HasGenericParams, PathSource, PatternSource};
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
use crate::access_levels::AccessLevelsVisitor;
@ -230,11 +230,11 @@ enum ResolutionError<'a> {
),
/// Error E0530: `X` bindings cannot shadow `Y`s.
BindingShadowsSomethingUnacceptable {
shadowing_binding_descr: &'static str,
shadowing_binding: PatternSource,
name: Symbol,
participle: &'static str,
article: &'static str,
shadowed_binding_descr: &'static str,
shadowed_binding: Res,
shadowed_binding_span: Span,
},
/// Error E0128: generic parameters with a default cannot use forward-declared identifiers.

View file

@ -5,7 +5,10 @@ LL | struct Empty2();
| ---------------- the tuple struct `Empty2` is defined here
...
LL | Empty2 => ()
| ^^^^^^ cannot be named the same as a tuple struct
| ^^^^^^
| |
| cannot be named the same as a tuple struct
| help: try specify the pattern arguments: `Empty2(..)`
error[E0530]: match bindings cannot shadow tuple structs
--> $DIR/empty-struct-tuple-pat.rs:25:9
@ -14,7 +17,10 @@ LL | use empty_struct::*;
| --------------- the tuple struct `XEmpty6` is imported here
...
LL | XEmpty6 => ()
| ^^^^^^^ cannot be named the same as a tuple struct
| ^^^^^^^
| |
| cannot be named the same as a tuple struct
| help: try specify the pattern arguments: `XEmpty6(..)`
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::Empty4`
--> $DIR/empty-struct-tuple-pat.rs:29:9

View file

@ -5,7 +5,10 @@ LL | use declarations_for_tuple_field_count_errors::*;
| -------------------------------------------- the tuple struct `Z1` is imported here
...
LL | Z1 => {}
| ^^ cannot be named the same as a tuple struct
| ^^
| |
| cannot be named the same as a tuple struct
| help: try specify the pattern arguments: `Z1(..)`
error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0`
--> $DIR/pat-tuple-field-count-cross.rs:9:9

View file

@ -5,7 +5,10 @@ LL | struct Z1();
| ------------ the tuple struct `Z1` is defined here
...
LL | Z1 => {}
| ^^ cannot be named the same as a tuple struct
| ^^
| |
| cannot be named the same as a tuple struct
| help: try specify the pattern arguments: `Z1(..)`
error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0`
--> $DIR/pat-tuple-overfield.rs:52:9

View file

@ -5,7 +5,10 @@ LL | struct TupleStruct();
| --------------------- the tuple struct `TupleStruct` is defined here
...
LL | TupleStruct => {}
| ^^^^^^^^^^^ cannot be named the same as a tuple struct
| ^^^^^^^^^^^
| |
| cannot be named the same as a tuple struct
| help: try specify the pattern arguments: `TupleStruct(..)`
error[E0530]: match bindings cannot shadow tuple variants
--> $DIR/pattern-binding-disambiguation.rs:33:9
@ -14,7 +17,10 @@ LL | use E::*;
| ---- the tuple variant `TupleVariant` is imported here
...
LL | TupleVariant => {}
| ^^^^^^^^^^^^ cannot be named the same as a tuple variant
| ^^^^^^^^^^^^
| |
| cannot be named the same as a tuple variant
| help: try specify the pattern arguments: `TupleVariant(..)`
error[E0530]: match bindings cannot shadow struct variants
--> $DIR/pattern-binding-disambiguation.rs:36:9