Error on bad pub(path) depends on module ordering
Which error message you get with a non-ancestral pub(path) visibility restriction depends on the ordering of module declarations.
These two struct declarations produce different errors;
mod foo {
pub(in ::bar) struct Foo;
}
mod bar {
pub(in ::foo) struct Bar;
}
The second produces the correct error:
error: visibilities can only be restricted to ancestor modules
The first produces this, incorrect error:
error[E0578]: cannot find module `bar` in the crate root
Presumably the pub(path) code is operating on the assumption the path, if real, has already been walked (because any correct path would have been walked), but this is not the case.
I've stumbled across this problem earlier today, see my dupe report above. It was my first attempt at using that feature, and I wasn't aware that only ancestor modules are allowed, and this issue added to the confusion.
est31 at 2017-03-21 10:10:11
This bug still exists. The syntax of path visibilities has changed to require
in, but updating the code sample topub(in ::foo)andpub(in ::bar), the error message is still order dependent.srrrse at 2017-09-19 17:39:48
@withoutboats: thanks for reviewing, I'll write mentoring instructions then.
Esteban Kuber at 2017-09-19 21:52:12
Mentoring instructions:
Resolver::resolve_visibility()is the method responsible for raising visibility issues, which callsResolver::smart_resolve_path(), the method that raises E0578.smart_resolve_path_fragmentshould be changed so it takes into consideration wethersourceis aPathSource::Visibilityinreport_errorsso that instead of adding aUseErrortouse_injection(which are picked up byreport_with_use_injectionsto suggest imports), we keep all the information needed to look for the (presumably) not yet seenmodand look for it after all checks have been performed (you probably would want to do it inreport_errors). If you can find themod, then raise an incorrect visibility restriction error, otherwise raiseE0578(You can keep around the originalE0578diagnostic and.cancel()it if you can find the mod afterwards, or emit it otherwise).Esteban Kuber at 2017-09-21 18:12:03
Bug still exists in 2018 edition but you have to write
in crate::...jethrogb at 2019-03-28 20:16:41
Triage: no change.
Esteban Kuber at 2023-06-29 20:42:39
https://github.com/rust-lang/rust/issues/109657 is a duplicate of this issue and contains some explanation for the suboptimal diagnostics here.
Vadim Petrochenkov at 2023-06-29 20:56:43