Skip to content

Commit

Permalink
Improve diagnostic by suggesting to remove visibility qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohei316 committed Apr 12, 2024
1 parent 30840c5 commit ac1bee6
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 44 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ ast_passes_visibility_not_permitted =
.trait_impl = trait items always share the visibility of their trait
.individual_impl_items = place qualifiers on individual impl items instead
.individual_foreign_items = place qualifiers on individual foreign items instead
.remove_qualifier_sugg = remove the qualifier
ast_passes_where_clause_after_type_alias = where clauses are not allowed after the type for type aliases
.note = see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ impl<'a> AstValidator<'a> {
return;
}

self.dcx().emit_err(errors::VisibilityNotPermitted { span: vis.span, note });
self.dcx().emit_err(errors::VisibilityNotPermitted {
span: vis.span,
note,
remove_qualifier_sugg: vis.span,
});
}

fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub struct VisibilityNotPermitted {
pub span: Span,
#[subdiagnostic]
pub note: VisibilityNotPermittedNote,
#[suggestion(
ast_passes_remove_qualifier_sugg,
code = "",
applicability = "machine-applicable"
)]
pub remove_qualifier_sugg: Span,
}

#[derive(Subdiagnostic)]
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/error-codes/E0449.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ run-rustfix

#![allow(warnings)]

struct Bar;

trait Foo {
fn foo();
}

impl Bar {} //~ ERROR E0449

impl Foo for Bar { //~ ERROR E0449
fn foo() {} //~ ERROR E0449
}

fn main() {
}
4 changes: 4 additions & 0 deletions tests/ui/error-codes/E0449.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//@ run-rustfix

#![allow(warnings)]

struct Bar;

trait Foo {
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/error-codes/E0449.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
error[E0449]: visibility qualifiers are not permitted here
--> $DIR/E0449.rs:7:1
--> $DIR/E0449.rs:11:1
|
LL | pub impl Bar {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual impl items instead

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-28433.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-28433.rs:2:5
|
LL | pub Duck,
| ^^^
| ^^^ help: remove the qualifier
|
= note: enum variants and their fields always share the visibility of the enum they are in

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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/assoc/assoc-static-semantic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/assoc-static-semantic-fail.rs:32:5
|
LL | pub(crate) default static TD: u8;
| ^^^^^^^^^^
| ^^^^^^^^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/default.rs:17:5
|
LL | pub default fn foo<T: Default>() -> T {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/trait-pub-assoc-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/trait-pub-assoc-const.rs:2:5
|
LL | pub const Foo: u32;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/trait-pub-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/trait-pub-assoc-ty.rs:2:5
|
LL | pub type Foo;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/trait-pub-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/trait-pub-method.rs:2:5
|
LL | pub fn foo();
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/privacy/issue-113860-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860-1.rs:12:5
|
LL | pub(self) fn fun() {}
| ^^^^^^^^^
| ^^^^^^^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/privacy/issue-113860-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860-2.rs:12:5
|
LL | pub(self) type X = Self;
| ^^^^^^^^^
| ^^^^^^^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/privacy/issue-113860.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860.rs:12:5
|
LL | pub(self) const X: u32 = 3;
| ^^^^^^^^^
| ^^^^^^^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/privacy/issue-29161.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-29161.rs:5:9
|
LL | pub fn default() -> A {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/privacy/priv-in-bad-locations.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/priv-in-bad-locations.rs:1:1
|
LL | pub extern "C" {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual foreign items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/priv-in-bad-locations.rs:11:1
|
LL | pub impl B {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual impl items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/priv-in-bad-locations.rs:13:1
|
LL | pub impl A for B {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/priv-in-bad-locations.rs:14:5
|
LL | pub fn foo(&self) {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

Expand Down
36 changes: 18 additions & 18 deletions tests/ui/privacy/privacy-sanity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,143 +2,143 @@ error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:13:1
|
LL | pub impl Tr for S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:14:5
|
LL | pub fn f() {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:15:5
|
LL | pub const C: u8 = 0;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:16:5
|
LL | pub type T = u8;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:18:1
|
LL | pub impl S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual impl items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:23:1
|
LL | pub extern "C" {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual foreign items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:39:5
|
LL | pub impl Tr for S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:40:9
|
LL | pub fn f() {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:41:9
|
LL | pub const C: u8 = 0;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:42:9
|
LL | pub type T = u8;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:44:5
|
LL | pub impl S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual impl items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:49:5
|
LL | pub extern "C" {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual foreign items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:68:5
|
LL | pub impl Tr for S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:69:9
|
LL | pub fn f() {}
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:70:9
|
LL | pub const C: u8 = 0;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:71:9
|
LL | pub type T = u8;
| ^^^
| ^^^ help: remove the qualifier
|
= note: trait items always share the visibility of their trait

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:73:5
|
LL | pub impl S {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual impl items instead

error[E0449]: visibility qualifiers are not permitted here
--> $DIR/privacy-sanity.rs:78:5
|
LL | pub extern "C" {
| ^^^
| ^^^ help: remove the qualifier
|
= note: place qualifiers on individual foreign items instead

Expand Down
Loading

0 comments on commit ac1bee6

Please sign in to comment.