]> git.lizzy.rs Git - rust.git/commit
fallback to dir_path when relative external mod resolution fails
authorYacin Tmimi <yacintmimi@gmail.com>
Sun, 30 Jan 2022 18:46:53 +0000 (13:46 -0500)
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>
Tue, 1 Mar 2022 05:13:11 +0000 (23:13 -0600)
commit12048e444f8a68b3c13e98b15c11a69de9f0b485
treed348dafe40ed35d3fcd63c9e72f97be5f06b327c
parent89ca3f3a100456d652b156b1a62b8e244e526c4e
fallback to dir_path when relative external mod resolution fails

We only want to fall back if two conditions are met:

1) Initial module resolution is performed relative to some nested
   directory.
2) Module resolution fails because of a ModError::FileNotFound error.

When these conditions are met we can try to fallback to searching for
the module's file relative to the dir_path instead of the nested
relative directory.

Fixes 5198

As demonstrated by 5198, it's possible that a directory name conflicts
with a rust file name. For example, src/lib/ and src/lib.rs.

If src/lib.rs references an external module like ``mod foo;``, then
module resolution will try to resolve ``foo`` to src/lib/foo.rs or
src/lib/foo/mod.rs. Module resolution would fail with a file not
found error if the ``foo`` module were defined at src/foo.rs.

When encountering these kinds of module resolution issues we now fall
back to the current directory and attempt to resolve the module again.

Given the current example, this means that if we can't find the module
``foo`` at src/lib/foo.rs or src/lib/foo/mod.rs, we'll attempt
to resolve the module to src/foo.rs.
12 files changed:
src/parse/session.rs
src/test/mod_resolver.rs
tests/mod-resolver/issue-5198/a.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/b.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/d.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/d/explanation.txt [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/d/f.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/d/g/mod.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/e.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/c/mod.rs [new file with mode: 0644]
tests/mod-resolver/issue-5198/lib/explanation.txt [new file with mode: 0644]