Skip to content

Commit

Permalink
Rollup merge of #113168 - bvanjoi:fix-85992, r=petrochenkov
Browse files Browse the repository at this point in the history
fix(resolve): skip assertion judgment when NonModule is dummy

Fixes #85992

## Why #85992 panic

During `resolve_imports`, the `path_res` of the import `issue_85992_extern_2::Outcome` is pointing to `external::issue_85992_extern_2` instead of `crate::issue_85992_extern_2`. As a result `import.imported_module.set` had been executed.

Attached 1: the state of `early_resolve_ident_in_lexical_scope` during the `resolve_imports` for `use issue_85992_extern_2::Outcome` is as follows:

|iter in `visit_scopes`  | `scope` | `result.binding` |
| -    | -               | -                                                            |
| init | -               | -                                                            |
| 0    | `CrateRoot`     | Err(Determined)     |
| 1    | `ExternPrelude` | pointing to the `issue_85992_extern_2`(external) |

However, during finalization for `issue_85992_extern_2::Outcome`, the `innermost_result` was pointed to `crate::issue_85992_extern_2` and no ambiguity was generated, leading to a panic.

Attached 2: the state of `early_resolve_ident_in_lexical_scope` during the `finalize_import` for `use issue_85992_extern_2::Outcome` is as follows:

|iter in `visit_scopes`  | `scope` | `result.binding` | `innermost_result` |
| -    | -               | -                                                            | -                     |
| init | -               | -                                                            | `None`                |
| 0    | `CrateRoot`     | pointing to `use crate::issue_85992_extern_2` **(introdcued by dummy)**    | same as `result` but with a `Some` wapper|
| 1    | `ExternPrelude` | pointing to the `issue_85992_extern_2`(external) | smae as above |

## Try to solve

Skip assertion judgment when `NonModule` is dummy

r? `@petrochenkov`
  • Loading branch information
matthiaskrgr committed Jul 1, 2023
2 parents e5bb341 + 549f48d commit b63bc06
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// expansion and import resolution (perhaps they can be merged in the future).
/// The function is used for resolving initial segments of macro paths (e.g., `foo` in
/// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition.
#[instrument(level = "debug", skip(self, scope_set))]
#[instrument(level = "debug", skip(self))]
pub(crate) fn early_resolve_ident_in_lexical_scope(
&mut self,
orig_ident: Ident,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
return None;
}
PathResult::NonModule(_) => {
if no_ambiguity {
PathResult::NonModule(partial_res) => {
if no_ambiguity && partial_res.full_res() != Some(Res::Err) {
// Check if there are no ambiguities and the result is not dummy.
assert!(import.imported_module.get().is_none());
}
// The error was already reported earlier.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ enum Scope<'a> {
/// with different restrictions when looking up the resolution.
/// This enum is currently used only for early resolution (imports and macros),
/// but not for late resolution yet.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum ScopeSet<'a> {
/// All scopes with the given namespace.
All(Namespace),
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/imports/auxiliary/issue-85992-extern-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_export]
macro_rules! m {
() => {
use issue_85992_extern_2::Outcome;
}
}
1 change: 1 addition & 0 deletions tests/ui/imports/auxiliary/issue-85992-extern-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// nothing
11 changes: 11 additions & 0 deletions tests/ui/imports/issue-85992.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// edition: 2021
// compile-flags: --extern issue_85992_extern_1 --extern issue_85992_extern_2
// aux-build: issue-85992-extern-1.rs
// aux-build: issue-85992-extern-2.rs

issue_85992_extern_1::m!();

use crate::issue_85992_extern_2;
//~^ ERROR unresolved import `crate::issue_85992_extern_2`

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/imports/issue-85992.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0432]: unresolved import `crate::issue_85992_extern_2`
--> $DIR/issue-85992.rs:8:5
|
LL | use crate::issue_85992_extern_2;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `issue_85992_extern_2` in the root

error: aborting due to previous error

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

0 comments on commit b63bc06

Please sign in to comment.