From c8420db21d6d64ed9ce8c7937ad7ceda0eee8d20 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 14 Oct 2019 13:13:38 +0200 Subject: [PATCH 1/4] Create new error code E0740 for visibility restrictions to ancestor module issues --- src/librustc_resolve/build_reduced_graph.rs | 6 +++--- src/librustc_resolve/error_codes.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index c18bdfad22cf0..3564aefba73ae 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -256,9 +256,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { if self.r.is_accessible_from(vis, parent_scope.module) { vis } else { - let msg = - "visibilities can only be restricted to ancestor modules"; - self.r.session.span_err(path.span, msg); + struct_span_err!(self.r.session, path.span, E0741, + "visibilities can only be restricted to ancestor modules") + .emit(); ty::Visibility::Public } } diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index 9515c87ce4bb5..b76373703d1ea 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1953,4 +1953,5 @@ struct Foo> { // E0470, removed E0577, E0578, + E0740, } From 208af201ec8a2fc563d58005b1609e02fce9ab5e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 14 Oct 2019 14:06:54 +0200 Subject: [PATCH 2/4] Add long error explanation for E0740 --- src/librustc_resolve/error_codes.rs | 39 ++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index b76373703d1ea..32e5eb50a76da 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1933,6 +1933,44 @@ struct Foo> { ``` "##, +E0741: r##" +Visibility is restricted to a module which isn't an ancestor of the current +item. + +Erroneous code example: + +```compile_fail,E0741,edition2018 +pub mod Sea {} + +pub (in crate::Sea) struct Shark; // error! + +fn main() {} +``` + +To fix this error, we need to move the `Shark` struct inside the `Sea` module: + +```edition2018 +pub mod Sea { + pub (in crate::Sea) struct Shark; // ok! +} + +fn main() {} +``` + +Of course, you can do it as long as the module you're referring to is an +ancestor: + +```edition2018 +pub mod Earth { + pub mod Sea { + pub (in crate::Earth) struct Shark; // ok! + } +} + +fn main() {} +``` +"##, + ; // E0153, unused error code // E0157, unused error code @@ -1953,5 +1991,4 @@ struct Foo> { // E0470, removed E0577, E0578, - E0740, } From 6c7fe5a94ee1c6c22ec6c2c6e986ea68f8b3e7f6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 14 Oct 2019 14:07:05 +0200 Subject: [PATCH 3/4] Update ui tests --- src/test/ui/privacy/restricted/relative-2018.stderr | 3 ++- src/test/ui/privacy/restricted/test.stderr | 4 ++-- src/test/ui/proc-macro/issue-50493.stderr | 5 +++-- src/test/ui/pub/pub-restricted.stderr | 5 +++-- src/test/ui/resolve/resolve-bad-visibility.stderr | 5 +++-- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/test/ui/privacy/restricted/relative-2018.stderr b/src/test/ui/privacy/restricted/relative-2018.stderr index 61effc463e98f..2205d6e518cf3 100644 --- a/src/test/ui/privacy/restricted/relative-2018.stderr +++ b/src/test/ui/privacy/restricted/relative-2018.stderr @@ -1,4 +1,4 @@ -error: visibilities can only be restricted to ancestor modules +error[E0741]: visibilities can only be restricted to ancestor modules --> $DIR/relative-2018.rs:7:12 | LL | pub(in ::core) struct S4; @@ -14,3 +14,4 @@ LL | pub(in a::b) struct S5; error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0741`. diff --git a/src/test/ui/privacy/restricted/test.stderr b/src/test/ui/privacy/restricted/test.stderr index a23973d0853f1..b39a42ef3ce3b 100644 --- a/src/test/ui/privacy/restricted/test.stderr +++ b/src/test/ui/privacy/restricted/test.stderr @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: maybe a missing crate `bad`? LL | pub(in bad::path) mod m1 {} | ^^^ maybe a missing crate `bad`? -error: visibilities can only be restricted to ancestor modules +error[E0741]: visibilities can only be restricted to ancestor modules --> $DIR/test.rs:51:12 | LL | pub(in foo) mod m2 {} @@ -78,5 +78,5 @@ LL | u.h(); error: aborting due to 12 previous errors -Some errors have detailed explanations: E0364, E0433, E0603, E0616, E0624. +Some errors have detailed explanations: E0364, E0433, E0603, E0616, E0624, E0741. For more information about an error, try `rustc --explain E0364`. diff --git a/src/test/ui/proc-macro/issue-50493.stderr b/src/test/ui/proc-macro/issue-50493.stderr index 28c61a2f52fed..25035106f25c9 100644 --- a/src/test/ui/proc-macro/issue-50493.stderr +++ b/src/test/ui/proc-macro/issue-50493.stderr @@ -1,4 +1,4 @@ -error: visibilities can only be restricted to ancestor modules +error[E0741]: visibilities can only be restricted to ancestor modules --> $DIR/issue-50493.rs:8:12 | LL | pub(in restricted) field: usize, @@ -12,4 +12,5 @@ LL | #[derive(Derive)] error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0616`. +Some errors have detailed explanations: E0616, E0741. +For more information about an error, try `rustc --explain E0616`. diff --git a/src/test/ui/pub/pub-restricted.stderr b/src/test/ui/pub/pub-restricted.stderr index 596264ba16b42..3ec4e1143feba 100644 --- a/src/test/ui/pub/pub-restricted.stderr +++ b/src/test/ui/pub/pub-restricted.stderr @@ -53,7 +53,7 @@ LL | pub (xyz) fn xyz() {} `pub(super)`: visible only in the current module's parent `pub(in path::to::module)`: visible only on the specified path -error: visibilities can only be restricted to ancestor modules +error[E0741]: visibilities can only be restricted to ancestor modules --> $DIR/pub-restricted.rs:25:17 | LL | pub (in x) non_parent_invalid: usize, @@ -61,4 +61,5 @@ LL | pub (in x) non_parent_invalid: usize, error: aborting due to 6 previous errors -For more information about this error, try `rustc --explain E0704`. +Some errors have detailed explanations: E0704, E0741. +For more information about an error, try `rustc --explain E0704`. diff --git a/src/test/ui/resolve/resolve-bad-visibility.stderr b/src/test/ui/resolve/resolve-bad-visibility.stderr index d2fb7c7a9e69d..b077bb81872a8 100644 --- a/src/test/ui/resolve/resolve-bad-visibility.stderr +++ b/src/test/ui/resolve/resolve-bad-visibility.stderr @@ -10,7 +10,7 @@ error[E0577]: expected module, found trait `Tr` LL | pub(in Tr) struct Z; | ^^ not a module -error: visibilities can only be restricted to ancestor modules +error[E0741]: visibilities can only be restricted to ancestor modules --> $DIR/resolve-bad-visibility.rs:6:8 | LL | pub(in std::vec) struct F; @@ -30,4 +30,5 @@ LL | pub(in too_soon) struct H; error: aborting due to 5 previous errors -For more information about this error, try `rustc --explain E0433`. +Some errors have detailed explanations: E0433, E0741. +For more information about an error, try `rustc --explain E0433`. From 9869e5b969553699af4f50433170827d4e440e60 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 29 Oct 2019 13:59:22 +0100 Subject: [PATCH 4/4] Change E0741 into E0742 --- src/librustc_resolve/build_reduced_graph.rs | 2 +- src/librustc_resolve/error_codes.rs | 5 +++-- src/test/ui/privacy/restricted/relative-2018.stderr | 4 ++-- src/test/ui/privacy/restricted/test.stderr | 4 ++-- src/test/ui/proc-macro/issue-50493.stderr | 4 ++-- src/test/ui/pub/pub-restricted.stderr | 4 ++-- src/test/ui/resolve/resolve-bad-visibility.stderr | 4 ++-- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 3564aefba73ae..648c5104b1af7 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -256,7 +256,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { if self.r.is_accessible_from(vis, parent_scope.module) { vis } else { - struct_span_err!(self.r.session, path.span, E0741, + struct_span_err!(self.r.session, path.span, E0742, "visibilities can only be restricted to ancestor modules") .emit(); ty::Visibility::Public diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index 32e5eb50a76da..9937f27931fb7 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1910,6 +1910,7 @@ E0671: r##" Const parameters cannot depend on type parameters. The following is therefore invalid: + ```compile_fail,E0741 #![feature(const_generics)] @@ -1933,13 +1934,13 @@ struct Foo> { ``` "##, -E0741: r##" +E0742: r##" Visibility is restricted to a module which isn't an ancestor of the current item. Erroneous code example: -```compile_fail,E0741,edition2018 +```compile_fail,E0742,edition2018 pub mod Sea {} pub (in crate::Sea) struct Shark; // error! diff --git a/src/test/ui/privacy/restricted/relative-2018.stderr b/src/test/ui/privacy/restricted/relative-2018.stderr index 2205d6e518cf3..54fee085ee943 100644 --- a/src/test/ui/privacy/restricted/relative-2018.stderr +++ b/src/test/ui/privacy/restricted/relative-2018.stderr @@ -1,4 +1,4 @@ -error[E0741]: visibilities can only be restricted to ancestor modules +error[E0742]: visibilities can only be restricted to ancestor modules --> $DIR/relative-2018.rs:7:12 | LL | pub(in ::core) struct S4; @@ -14,4 +14,4 @@ LL | pub(in a::b) struct S5; error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0741`. +For more information about this error, try `rustc --explain E0742`. diff --git a/src/test/ui/privacy/restricted/test.stderr b/src/test/ui/privacy/restricted/test.stderr index b39a42ef3ce3b..e6a61fbefb0d8 100644 --- a/src/test/ui/privacy/restricted/test.stderr +++ b/src/test/ui/privacy/restricted/test.stderr @@ -4,7 +4,7 @@ error[E0433]: failed to resolve: maybe a missing crate `bad`? LL | pub(in bad::path) mod m1 {} | ^^^ maybe a missing crate `bad`? -error[E0741]: visibilities can only be restricted to ancestor modules +error[E0742]: visibilities can only be restricted to ancestor modules --> $DIR/test.rs:51:12 | LL | pub(in foo) mod m2 {} @@ -78,5 +78,5 @@ LL | u.h(); error: aborting due to 12 previous errors -Some errors have detailed explanations: E0364, E0433, E0603, E0616, E0624, E0741. +Some errors have detailed explanations: E0364, E0433, E0603, E0616, E0624, E0742. For more information about an error, try `rustc --explain E0364`. diff --git a/src/test/ui/proc-macro/issue-50493.stderr b/src/test/ui/proc-macro/issue-50493.stderr index 25035106f25c9..6b8724457a6a3 100644 --- a/src/test/ui/proc-macro/issue-50493.stderr +++ b/src/test/ui/proc-macro/issue-50493.stderr @@ -1,4 +1,4 @@ -error[E0741]: visibilities can only be restricted to ancestor modules +error[E0742]: visibilities can only be restricted to ancestor modules --> $DIR/issue-50493.rs:8:12 | LL | pub(in restricted) field: usize, @@ -12,5 +12,5 @@ LL | #[derive(Derive)] error: aborting due to 2 previous errors -Some errors have detailed explanations: E0616, E0741. +Some errors have detailed explanations: E0616, E0742. For more information about an error, try `rustc --explain E0616`. diff --git a/src/test/ui/pub/pub-restricted.stderr b/src/test/ui/pub/pub-restricted.stderr index 3ec4e1143feba..56ff104b4fed9 100644 --- a/src/test/ui/pub/pub-restricted.stderr +++ b/src/test/ui/pub/pub-restricted.stderr @@ -53,7 +53,7 @@ LL | pub (xyz) fn xyz() {} `pub(super)`: visible only in the current module's parent `pub(in path::to::module)`: visible only on the specified path -error[E0741]: visibilities can only be restricted to ancestor modules +error[E0742]: visibilities can only be restricted to ancestor modules --> $DIR/pub-restricted.rs:25:17 | LL | pub (in x) non_parent_invalid: usize, @@ -61,5 +61,5 @@ LL | pub (in x) non_parent_invalid: usize, error: aborting due to 6 previous errors -Some errors have detailed explanations: E0704, E0741. +Some errors have detailed explanations: E0704, E0742. For more information about an error, try `rustc --explain E0704`. diff --git a/src/test/ui/resolve/resolve-bad-visibility.stderr b/src/test/ui/resolve/resolve-bad-visibility.stderr index b077bb81872a8..43af38cf491e3 100644 --- a/src/test/ui/resolve/resolve-bad-visibility.stderr +++ b/src/test/ui/resolve/resolve-bad-visibility.stderr @@ -10,7 +10,7 @@ error[E0577]: expected module, found trait `Tr` LL | pub(in Tr) struct Z; | ^^ not a module -error[E0741]: visibilities can only be restricted to ancestor modules +error[E0742]: visibilities can only be restricted to ancestor modules --> $DIR/resolve-bad-visibility.rs:6:8 | LL | pub(in std::vec) struct F; @@ -30,5 +30,5 @@ LL | pub(in too_soon) struct H; error: aborting due to 5 previous errors -Some errors have detailed explanations: E0433, E0741. +Some errors have detailed explanations: E0433, E0742. For more information about an error, try `rustc --explain E0433`.