Skip to content

Commit 6a86be9

Browse files
authored
Rollup merge of rust-lang#60429 - estebank:pub-path, r=michaelwoerister
Account for paths in incorrect pub qualifier help Handle case where incorrect pub qualifier with a mod path is used and provide the same help given for all other incorrect qualifiers by making the `pub(crate)` parse check more specific.
2 parents e232636 + a3aafea commit 6a86be9

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/libsyntax/parse/parser.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -7149,7 +7149,9 @@ impl<'a> Parser<'a> {
71497149
// `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
71507150
// Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
71517151
// by the following tokens.
7152-
if self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) {
7152+
if self.look_ahead(1, |t| t.is_keyword(keywords::Crate)) &&
7153+
self.look_ahead(2, |t| t != &token::ModSep) // account for `pub(crate::foo)`
7154+
{
71537155
// `pub(crate)`
71547156
self.bump(); // `(`
71557157
self.bump(); // `crate`
@@ -7192,7 +7194,7 @@ impl<'a> Parser<'a> {
71927194
`pub(super)`: visible only in the current module's parent
71937195
`pub(in path::to::module)`: visible only on the specified path"##;
71947196
let path = self.parse_path(PathStyle::Mod)?;
7195-
let sp = self.prev_span;
7197+
let sp = path.span;
71967198
let help_msg = format!("make this visible only to module `{}` with `in`", path);
71977199
self.expect(&token::CloseDelim(token::Paren))?; // `)`
71987200
let mut err = struct_span_err!(self.sess.span_diagnostic, sp, E0704, "{}", msg);

src/test/ui/pub/pub-restricted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ mod a {}
44

55
pub (a) fn afn() {} //~ incorrect visibility restriction
66
pub (b) fn bfn() {} //~ incorrect visibility restriction
7+
pub (crate::a) fn cfn() {} //~ incorrect visibility restriction
8+
79
pub fn privfn() {}
810
mod x {
911
mod y {

src/test/ui/pub/pub-restricted.stderr

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ LL | pub (b) fn bfn() {}
2121
`pub(in path::to::module)`: visible only on the specified path
2222

2323
error[E0704]: incorrect visibility restriction
24-
--> $DIR/pub-restricted.rs:22:14
24+
--> $DIR/pub-restricted.rs:7:6
25+
|
26+
LL | pub (crate::a) fn cfn() {}
27+
| ^^^^^^^^ help: make this visible only to module `crate::a` with `in`: `in crate::a`
28+
|
29+
= help: some possible visibility restrictions are:
30+
`pub(crate)`: visible only on the current crate
31+
`pub(super)`: visible only in the current module's parent
32+
`pub(in path::to::module)`: visible only on the specified path
33+
34+
error[E0704]: incorrect visibility restriction
35+
--> $DIR/pub-restricted.rs:24:14
2536
|
2637
LL | pub (a) invalid: usize,
2738
| ^ help: make this visible only to module `a` with `in`: `in a`
@@ -32,7 +43,7 @@ LL | pub (a) invalid: usize,
3243
`pub(in path::to::module)`: visible only on the specified path
3344

3445
error[E0704]: incorrect visibility restriction
35-
--> $DIR/pub-restricted.rs:31:6
46+
--> $DIR/pub-restricted.rs:33:6
3647
|
3748
LL | pub (xyz) fn xyz() {}
3849
| ^^^ help: make this visible only to module `xyz` with `in`: `in xyz`
@@ -43,11 +54,11 @@ LL | pub (xyz) fn xyz() {}
4354
`pub(in path::to::module)`: visible only on the specified path
4455

4556
error: visibilities can only be restricted to ancestor modules
46-
--> $DIR/pub-restricted.rs:23:17
57+
--> $DIR/pub-restricted.rs:25:17
4758
|
4859
LL | pub (in x) non_parent_invalid: usize,
4960
| ^
5061

51-
error: aborting due to 5 previous errors
62+
error: aborting due to 6 previous errors
5263

5364
For more information about this error, try `rustc --explain E0704`.

0 commit comments

Comments
 (0)