Skip to content

Commit d75a572

Browse files
authored
Rollup merge of #99008 - obeis:issue-98974, r=compiler-errors
Adding suggestion for E0530 Closes #98974
2 parents a6c6166 + 1b32eb3 commit d75a572

7 files changed

+52
-19
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_span::{BytePos, Span};
2828
use tracing::debug;
2929

3030
use crate::imports::{Import, ImportKind, ImportResolver};
31-
use crate::late::Rib;
31+
use crate::late::{PatternSource, Rib};
3232
use crate::path_names_to_string;
3333
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
3434
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
@@ -896,25 +896,40 @@ impl<'a> Resolver<'a> {
896896
err
897897
}
898898
ResolutionError::BindingShadowsSomethingUnacceptable {
899-
shadowing_binding_descr,
899+
shadowing_binding,
900900
name,
901901
participle,
902902
article,
903-
shadowed_binding_descr,
903+
shadowed_binding,
904904
shadowed_binding_span,
905905
} => {
906+
let shadowed_binding_descr = shadowed_binding.descr();
906907
let mut err = struct_span_err!(
907908
self.session,
908909
span,
909910
E0530,
910911
"{}s cannot shadow {}s",
911-
shadowing_binding_descr,
912+
shadowing_binding.descr(),
912913
shadowed_binding_descr,
913914
);
914915
err.span_label(
915916
span,
916917
format!("cannot be named the same as {} {}", article, shadowed_binding_descr),
917918
);
919+
match (shadowing_binding, shadowed_binding) {
920+
(
921+
PatternSource::Match,
922+
Res::Def(DefKind::Ctor(CtorOf::Variant | CtorOf::Struct, CtorKind::Fn), _),
923+
) => {
924+
err.span_suggestion(
925+
span,
926+
"try specify the pattern arguments",
927+
format!("{}(..)", name),
928+
Applicability::Unspecified,
929+
);
930+
}
931+
_ => (),
932+
}
918933
let msg =
919934
format!("the {} `{}` is {} here", shadowed_binding_descr, name, participle);
920935
err.span_label(shadowed_binding_span, msg);

compiler/rustc_resolve/src/late.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct BindingInfo {
5050
}
5151

5252
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
53-
enum PatternSource {
53+
pub enum PatternSource {
5454
Match,
5555
Let,
5656
For,
@@ -64,7 +64,7 @@ enum IsRepeatExpr {
6464
}
6565

6666
impl PatternSource {
67-
fn descr(self) -> &'static str {
67+
pub fn descr(self) -> &'static str {
6868
match self {
6969
PatternSource::Match => "match binding",
7070
PatternSource::Let => "let binding",
@@ -2845,11 +2845,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
28452845
self.report_error(
28462846
ident.span,
28472847
ResolutionError::BindingShadowsSomethingUnacceptable {
2848-
shadowing_binding_descr: pat_src.descr(),
2848+
shadowing_binding: pat_src,
28492849
name: ident.name,
28502850
participle: if binding.is_import() { "imported" } else { "defined" },
28512851
article: binding.res().article(),
2852-
shadowed_binding_descr: binding.res().descr(),
2852+
shadowed_binding: binding.res(),
28532853
shadowed_binding_span: binding.span,
28542854
},
28552855
);
@@ -2861,11 +2861,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
28612861
self.report_error(
28622862
ident.span,
28632863
ResolutionError::BindingShadowsSomethingUnacceptable {
2864-
shadowing_binding_descr: pat_src.descr(),
2864+
shadowing_binding: pat_src,
28652865
name: ident.name,
28662866
participle: "defined",
28672867
article: res.article(),
2868-
shadowed_binding_descr: res.descr(),
2868+
shadowed_binding: res,
28692869
shadowed_binding_span: self.r.opt_span(def_id).expect("const parameter defined outside of local crate"),
28702870
}
28712871
);

compiler/rustc_resolve/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use tracing::debug;
6161

6262
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
6363
use imports::{Import, ImportKind, ImportResolver, NameResolution};
64-
use late::{HasGenericParams, PathSource};
64+
use late::{HasGenericParams, PathSource, PatternSource};
6565
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
6666

6767
use crate::access_levels::AccessLevelsVisitor;
@@ -230,11 +230,11 @@ enum ResolutionError<'a> {
230230
),
231231
/// Error E0530: `X` bindings cannot shadow `Y`s.
232232
BindingShadowsSomethingUnacceptable {
233-
shadowing_binding_descr: &'static str,
233+
shadowing_binding: PatternSource,
234234
name: Symbol,
235235
participle: &'static str,
236236
article: &'static str,
237-
shadowed_binding_descr: &'static str,
237+
shadowed_binding: Res,
238238
shadowed_binding_span: Span,
239239
},
240240
/// Error E0128: generic parameters with a default cannot use forward-declared identifiers.

src/test/ui/empty/empty-struct-tuple-pat.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | struct Empty2();
55
| ---------------- the tuple struct `Empty2` is defined here
66
...
77
LL | Empty2 => ()
8-
| ^^^^^^ cannot be named the same as a tuple struct
8+
| ^^^^^^
9+
| |
10+
| cannot be named the same as a tuple struct
11+
| help: try specify the pattern arguments: `Empty2(..)`
912

1013
error[E0530]: match bindings cannot shadow tuple structs
1114
--> $DIR/empty-struct-tuple-pat.rs:25:9
@@ -14,7 +17,10 @@ LL | use empty_struct::*;
1417
| --------------- the tuple struct `XEmpty6` is imported here
1518
...
1619
LL | XEmpty6 => ()
17-
| ^^^^^^^ cannot be named the same as a tuple struct
20+
| ^^^^^^^
21+
| |
22+
| cannot be named the same as a tuple struct
23+
| help: try specify the pattern arguments: `XEmpty6(..)`
1824

1925
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::Empty4`
2026
--> $DIR/empty-struct-tuple-pat.rs:29:9

src/test/ui/pattern/pat-tuple-field-count-cross.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | use declarations_for_tuple_field_count_errors::*;
55
| -------------------------------------------- the tuple struct `Z1` is imported here
66
...
77
LL | Z1 => {}
8-
| ^^ cannot be named the same as a tuple struct
8+
| ^^
9+
| |
10+
| cannot be named the same as a tuple struct
11+
| help: try specify the pattern arguments: `Z1(..)`
912

1013
error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0`
1114
--> $DIR/pat-tuple-field-count-cross.rs:9:9

src/test/ui/pattern/pat-tuple-overfield.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | struct Z1();
55
| ------------ the tuple struct `Z1` is defined here
66
...
77
LL | Z1 => {}
8-
| ^^ cannot be named the same as a tuple struct
8+
| ^^
9+
| |
10+
| cannot be named the same as a tuple struct
11+
| help: try specify the pattern arguments: `Z1(..)`
912

1013
error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0`
1114
--> $DIR/pat-tuple-overfield.rs:52:9

src/test/ui/pattern/pattern-binding-disambiguation.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | struct TupleStruct();
55
| --------------------- the tuple struct `TupleStruct` is defined here
66
...
77
LL | TupleStruct => {}
8-
| ^^^^^^^^^^^ cannot be named the same as a tuple struct
8+
| ^^^^^^^^^^^
9+
| |
10+
| cannot be named the same as a tuple struct
11+
| help: try specify the pattern arguments: `TupleStruct(..)`
912

1013
error[E0530]: match bindings cannot shadow tuple variants
1114
--> $DIR/pattern-binding-disambiguation.rs:33:9
@@ -14,7 +17,10 @@ LL | use E::*;
1417
| ---- the tuple variant `TupleVariant` is imported here
1518
...
1619
LL | TupleVariant => {}
17-
| ^^^^^^^^^^^^ cannot be named the same as a tuple variant
20+
| ^^^^^^^^^^^^
21+
| |
22+
| cannot be named the same as a tuple variant
23+
| help: try specify the pattern arguments: `TupleVariant(..)`
1824

1925
error[E0530]: match bindings cannot shadow struct variants
2026
--> $DIR/pattern-binding-disambiguation.rs:36:9

0 commit comments

Comments
 (0)