Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 15a8b49

Browse files
authored
Rollup merge of rust-lang#123841 - Kohei316:remove_qualifier_sugg, r=wesleywiser
Improve diagnostic by suggesting to remove visibility qualifier Resolves rust-lang#123529 This PR improve diagnostic by suggesting to remove visibility qualifier.
2 parents be3ea1d + ac1bee6 commit 15a8b49

19 files changed

+77
-44
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ ast_passes_visibility_not_permitted =
273273
.trait_impl = trait items always share the visibility of their trait
274274
.individual_impl_items = place qualifiers on individual impl items instead
275275
.individual_foreign_items = place qualifiers on individual foreign items instead
276+
.remove_qualifier_sugg = remove the qualifier
276277
277278
ast_passes_where_clause_after_type_alias = where clauses are not allowed after the type for type aliases
278279
.note = see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ impl<'a> AstValidator<'a> {
266266
return;
267267
}
268268

269-
self.dcx().emit_err(errors::VisibilityNotPermitted { span: vis.span, note });
269+
self.dcx().emit_err(errors::VisibilityNotPermitted {
270+
span: vis.span,
271+
note,
272+
remove_qualifier_sugg: vis.span,
273+
});
270274
}
271275

272276
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct VisibilityNotPermitted {
3131
pub span: Span,
3232
#[subdiagnostic]
3333
pub note: VisibilityNotPermittedNote,
34+
#[suggestion(
35+
ast_passes_remove_qualifier_sugg,
36+
code = "",
37+
applicability = "machine-applicable"
38+
)]
39+
pub remove_qualifier_sugg: Span,
3440
}
3541

3642
#[derive(Subdiagnostic)]

tests/ui/error-codes/E0449.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-rustfix
2+
3+
#![allow(warnings)]
4+
5+
struct Bar;
6+
7+
trait Foo {
8+
fn foo();
9+
}
10+
11+
impl Bar {} //~ ERROR E0449
12+
13+
impl Foo for Bar { //~ ERROR E0449
14+
fn foo() {} //~ ERROR E0449
15+
}
16+
17+
fn main() {
18+
}

tests/ui/error-codes/E0449.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//@ run-rustfix
2+
3+
#![allow(warnings)]
4+
15
struct Bar;
26

37
trait Foo {

tests/ui/error-codes/E0449.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error[E0449]: visibility qualifiers are not permitted here
2-
--> $DIR/E0449.rs:7:1
2+
--> $DIR/E0449.rs:11:1
33
|
44
LL | pub impl Bar {}
5-
| ^^^
5+
| ^^^ help: remove the qualifier
66
|
77
= note: place qualifiers on individual impl items instead
88

99
error[E0449]: visibility qualifiers are not permitted here
10-
--> $DIR/E0449.rs:9:1
10+
--> $DIR/E0449.rs:13:1
1111
|
1212
LL | pub impl Foo for Bar {
13-
| ^^^
13+
| ^^^ help: remove the qualifier
1414
|
1515
= note: trait items always share the visibility of their trait
1616

1717
error[E0449]: visibility qualifiers are not permitted here
18-
--> $DIR/E0449.rs:10:5
18+
--> $DIR/E0449.rs:14:5
1919
|
2020
LL | pub fn foo() {}
21-
| ^^^
21+
| ^^^ help: remove the qualifier
2222
|
2323
= note: trait items always share the visibility of their trait
2424

tests/ui/issues/issue-28433.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0449]: visibility qualifiers are not permitted here
22
--> $DIR/issue-28433.rs:2:5
33
|
44
LL | pub Duck,
5-
| ^^^
5+
| ^^^ help: remove the qualifier
66
|
77
= note: enum variants and their fields always share the visibility of the enum they are in
88

99
error[E0449]: visibility qualifiers are not permitted here
1010
--> $DIR/issue-28433.rs:5:5
1111
|
1212
LL | pub(crate) Dove
13-
| ^^^^^^^^^^
13+
| ^^^^^^^^^^ help: remove the qualifier
1414
|
1515
= note: enum variants and their fields always share the visibility of the enum they are in
1616

tests/ui/parser/assoc/assoc-static-semantic-fail.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ error[E0449]: visibility qualifiers are not permitted here
138138
--> $DIR/assoc-static-semantic-fail.rs:32:5
139139
|
140140
LL | pub(crate) default static TD: u8;
141-
| ^^^^^^^^^^
141+
| ^^^^^^^^^^ help: remove the qualifier
142142
|
143143
= note: trait items always share the visibility of their trait
144144

@@ -162,7 +162,7 @@ error[E0449]: visibility qualifiers are not permitted here
162162
--> $DIR/assoc-static-semantic-fail.rs:47:5
163163
|
164164
LL | pub default static TD: u8;
165-
| ^^^
165+
| ^^^ help: remove the qualifier
166166
|
167167
= note: trait items always share the visibility of their trait
168168

tests/ui/parser/default.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ error[E0449]: visibility qualifiers are not permitted here
2121
--> $DIR/default.rs:17:5
2222
|
2323
LL | pub default fn foo<T: Default>() -> T {
24-
| ^^^
24+
| ^^^ help: remove the qualifier
2525
|
2626
= note: trait items always share the visibility of their trait
2727

tests/ui/parser/trait-pub-assoc-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
22
--> $DIR/trait-pub-assoc-const.rs:2:5
33
|
44
LL | pub const Foo: u32;
5-
| ^^^
5+
| ^^^ help: remove the qualifier
66
|
77
= note: trait items always share the visibility of their trait
88

0 commit comments

Comments
 (0)