mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 21:15:19 +00:00
[ty] Fix duplicate diagnostics for unresolved module when an import from
statement imports multiple members (#17886)
This commit is contained in:
parent
457ec4dddd
commit
2f9992b6ef
17 changed files with 95 additions and 60 deletions
|
@ -276,7 +276,7 @@ fn cli_arguments_are_relative_to_the_current_directory() -> anyhow::Result<()> {
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 1
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
error: lint:unresolved-import: Cannot resolve import `utils`
|
error: lint:unresolved-import: Cannot resolve imported module `utils`
|
||||||
--> test.py:2:6
|
--> test.py:2:6
|
||||||
|
|
|
|
||||||
2 | from utils import add
|
2 | from utils import add
|
||||||
|
@ -452,7 +452,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 1
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
error: lint:unresolved-import: Cannot resolve import `does_not_exit`
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exit`
|
||||||
--> test.py:2:8
|
--> test.py:2:8
|
||||||
|
|
|
|
||||||
2 | import does_not_exit
|
2 | import does_not_exit
|
||||||
|
@ -499,7 +499,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
|
||||||
success: true
|
success: true
|
||||||
exit_code: 0
|
exit_code: 0
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
warning: lint:unresolved-import: Cannot resolve import `does_not_exit`
|
warning: lint:unresolved-import: Cannot resolve imported module `does_not_exit`
|
||||||
--> test.py:2:8
|
--> test.py:2:8
|
||||||
|
|
|
|
||||||
2 | import does_not_exit
|
2 | import does_not_exit
|
||||||
|
@ -1053,7 +1053,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
|
||||||
|
|
||||||
error: lint:unresolved-import: Cannot resolve import `main2`
|
error: lint:unresolved-import: Cannot resolve imported module `main2`
|
||||||
--> project/other.py:2:6
|
--> project/other.py:2:6
|
||||||
|
|
|
|
||||||
2 | from main2 import z # error: unresolved-import
|
2 | from main2 import z # error: unresolved-import
|
||||||
|
@ -1062,7 +1062,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
|
||||||
4 | print(z)
|
4 | print(z)
|
||||||
|
|
|
|
||||||
|
|
||||||
error: lint:unresolved-import: Cannot resolve import `does_not_exist`
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exist`
|
||||||
--> project/tests/test_main.py:2:8
|
--> project/tests/test_main.py:2:8
|
||||||
|
|
|
|
||||||
2 | import does_not_exist # error: unresolved-import
|
2 | import does_not_exist # error: unresolved-import
|
||||||
|
@ -1083,7 +1083,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 1
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
error: lint:unresolved-import: Cannot resolve import `main2`
|
error: lint:unresolved-import: Cannot resolve imported module `main2`
|
||||||
--> project/other.py:2:6
|
--> project/other.py:2:6
|
||||||
|
|
|
|
||||||
2 | from main2 import z # error: unresolved-import
|
2 | from main2 import z # error: unresolved-import
|
||||||
|
@ -1092,7 +1092,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
|
||||||
4 | print(z)
|
4 | print(z)
|
||||||
|
|
|
|
||||||
|
|
||||||
error: lint:unresolved-import: Cannot resolve import `does_not_exist`
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exist`
|
||||||
--> project/tests/test_main.py:2:8
|
--> project/tests/test_main.py:2:8
|
||||||
|
|
|
|
||||||
2 | import does_not_exist # error: unresolved-import
|
2 | import does_not_exist # error: unresolved-import
|
||||||
|
|
|
@ -16,7 +16,7 @@ def _(flag: bool):
|
||||||
## Calling with an unknown union
|
## Calling with an unknown union
|
||||||
|
|
||||||
```py
|
```py
|
||||||
from nonexistent import f # error: [unresolved-import] "Cannot resolve import `nonexistent`"
|
from nonexistent import f # error: [unresolved-import] "Cannot resolve imported module `nonexistent`"
|
||||||
|
|
||||||
def coinflip() -> bool:
|
def coinflip() -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -121,7 +121,7 @@ class C: ...
|
||||||
<!-- snapshot-diagnostics -->
|
<!-- snapshot-diagnostics -->
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`"
|
import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Unresolvable submodule imports
|
## Unresolvable submodule imports
|
||||||
|
@ -130,10 +130,10 @@ import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzq
|
||||||
|
|
||||||
```py
|
```py
|
||||||
# Topmost component resolvable, submodule not resolvable:
|
# Topmost component resolvable, submodule not resolvable:
|
||||||
import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`"
|
import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`"
|
||||||
|
|
||||||
# Topmost component unresolvable:
|
# Topmost component unresolvable:
|
||||||
import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`"
|
import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
|
||||||
```
|
```
|
||||||
|
|
||||||
`a/__init__.py`:
|
`a/__init__.py`:
|
||||||
|
@ -164,3 +164,15 @@ from AveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongP
|
||||||
|
|
||||||
reveal_type(Foo()) # revealed: Foo
|
reveal_type(Foo()) # revealed: Foo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Multiple objects imported from an unresolved module
|
||||||
|
|
||||||
|
<!-- snapshot-diagnostics -->
|
||||||
|
|
||||||
|
If multiple members are imported from a module that cannot be resolved, only a single diagnostic is
|
||||||
|
emitted for the `import from` statement:
|
||||||
|
|
||||||
|
```py
|
||||||
|
# error: [unresolved-import]
|
||||||
|
from does_not_exist import foo, bar, baz
|
||||||
|
```
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
## Unresolved import statement
|
## Unresolved import statement
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import bar # error: "Cannot resolve import `bar`"
|
import bar # error: "Cannot resolve imported module `bar`"
|
||||||
|
|
||||||
reveal_type(bar) # revealed: Unknown
|
reveal_type(bar) # revealed: Unknown
|
||||||
```
|
```
|
||||||
|
@ -11,7 +11,7 @@ reveal_type(bar) # revealed: Unknown
|
||||||
## Unresolved import from statement
|
## Unresolved import from statement
|
||||||
|
|
||||||
```py
|
```py
|
||||||
from bar import baz # error: "Cannot resolve import `bar`"
|
from bar import baz # error: "Cannot resolve imported module `bar`"
|
||||||
|
|
||||||
reveal_type(baz) # revealed: Unknown
|
reveal_type(baz) # revealed: Unknown
|
||||||
```
|
```
|
||||||
|
@ -34,7 +34,7 @@ reveal_type(thing) # revealed: Unknown
|
||||||
`a.py`:
|
`a.py`:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import foo as foo # error: "Cannot resolve import `foo`"
|
import foo as foo # error: "Cannot resolve imported module `foo`"
|
||||||
|
|
||||||
reveal_type(foo) # revealed: Unknown
|
reveal_type(foo) # revealed: Unknown
|
||||||
```
|
```
|
||||||
|
|
|
@ -1369,7 +1369,7 @@ If the module is unresolved, we emit a diagnostic just like for any other unreso
|
||||||
|
|
||||||
```py
|
```py
|
||||||
# TODO: not a great error message
|
# TODO: not a great error message
|
||||||
from foo import * # error: [unresolved-import] "Cannot resolve import `foo`"
|
from foo import * # error: [unresolved-import] "Cannot resolve imported module `foo`"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Nested scope
|
### Nested scope
|
||||||
|
|
|
@ -81,7 +81,7 @@ new_module: 3.11-
|
||||||
```py
|
```py
|
||||||
from old_module import OldClass
|
from old_module import OldClass
|
||||||
|
|
||||||
# error: [unresolved-import] "Cannot resolve import `new_module`"
|
# error: [unresolved-import] "Cannot resolve imported module `new_module`"
|
||||||
from new_module import NewClass
|
from new_module import NewClass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -313,7 +313,6 @@ reveal_type(Omelette.__mro__) # revealed: tuple[Literal[Omelette], Unknown, Lit
|
||||||
|
|
||||||
```py
|
```py
|
||||||
# error: [unresolved-import]
|
# error: [unresolved-import]
|
||||||
# error: [unresolved-import]
|
|
||||||
from does_not_exist import unknown_object_1, unknown_object_2
|
from does_not_exist import unknown_object_1, unknown_object_2
|
||||||
|
|
||||||
reveal_type(unknown_object_1) # revealed: Unknown
|
reveal_type(unknown_object_1) # revealed: Unknown
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
---
|
||||||
|
source: crates/ty_test/src/lib.rs
|
||||||
|
expression: snapshot
|
||||||
|
---
|
||||||
|
---
|
||||||
|
mdtest name: basic.md - Structures - Multiple objects imported from an unresolved module
|
||||||
|
mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python source files
|
||||||
|
|
||||||
|
## mdtest_snippet.py
|
||||||
|
|
||||||
|
```
|
||||||
|
1 | # error: [unresolved-import]
|
||||||
|
2 | from does_not_exist import foo, bar, baz
|
||||||
|
```
|
||||||
|
|
||||||
|
# Diagnostics
|
||||||
|
|
||||||
|
```
|
||||||
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exist`
|
||||||
|
--> src/mdtest_snippet.py:2:6
|
||||||
|
|
|
||||||
|
1 | # error: [unresolved-import]
|
||||||
|
2 | from does_not_exist import foo, bar, baz
|
||||||
|
| ^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
|
@ -12,16 +12,16 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
|
||||||
## mdtest_snippet.py
|
## mdtest_snippet.py
|
||||||
|
|
||||||
```
|
```
|
||||||
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`"
|
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
|
||||||
```
|
```
|
||||||
|
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `zqzqzqzqzqzqzq`
|
error: lint:unresolved-import: Cannot resolve imported module `zqzqzqzqzqzqzq`
|
||||||
--> src/mdtest_snippet.py:1:8
|
--> src/mdtest_snippet.py:1:8
|
||||||
|
|
|
|
||||||
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve import `zqzqzqzqzqzqzq`"
|
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
|
||||||
| ^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,10 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
|
||||||
|
|
||||||
```
|
```
|
||||||
1 | # Topmost component resolvable, submodule not resolvable:
|
1 | # Topmost component resolvable, submodule not resolvable:
|
||||||
2 | import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`"
|
2 | import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`"
|
||||||
3 |
|
3 |
|
||||||
4 | # Topmost component unresolvable:
|
4 | # Topmost component unresolvable:
|
||||||
5 | import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`"
|
5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
|
||||||
```
|
```
|
||||||
|
|
||||||
## a/__init__.py
|
## a/__init__.py
|
||||||
|
@ -27,11 +27,11 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `a.foo`
|
error: lint:unresolved-import: Cannot resolve imported module `a.foo`
|
||||||
--> src/mdtest_snippet.py:2:8
|
--> src/mdtest_snippet.py:2:8
|
||||||
|
|
|
|
||||||
1 | # Topmost component resolvable, submodule not resolvable:
|
1 | # Topmost component resolvable, submodule not resolvable:
|
||||||
2 | import a.foo # error: [unresolved-import] "Cannot resolve import `a.foo`"
|
2 | import a.foo # error: [unresolved-import] "Cannot resolve imported module `a.foo`"
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
3 |
|
3 |
|
||||||
4 | # Topmost component unresolvable:
|
4 | # Topmost component unresolvable:
|
||||||
|
@ -40,11 +40,11 @@ error: lint:unresolved-import: Cannot resolve import `a.foo`
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `b.foo`
|
error: lint:unresolved-import: Cannot resolve imported module `b.foo`
|
||||||
--> src/mdtest_snippet.py:5:8
|
--> src/mdtest_snippet.py:5:8
|
||||||
|
|
|
|
||||||
4 | # Topmost component unresolvable:
|
4 | # Topmost component unresolvable:
|
||||||
5 | import b.foo # error: [unresolved-import] "Cannot resolve import `b.foo`"
|
5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `does_not_exist`
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exist`
|
||||||
--> src/mdtest_snippet.py:1:8
|
--> src/mdtest_snippet.py:1:8
|
||||||
|
|
|
|
||||||
1 | import does_not_exist # error: [unresolved-import]
|
1 | import does_not_exist # error: [unresolved-import]
|
||||||
|
|
|
@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `.does_not_exist`
|
error: lint:unresolved-import: Cannot resolve imported module `.does_not_exist`
|
||||||
--> src/mdtest_snippet.py:1:7
|
--> src/mdtest_snippet.py:1:7
|
||||||
|
|
|
|
||||||
1 | from .does_not_exist import add # error: [unresolved-import]
|
1 | from .does_not_exist import add # error: [unresolved-import]
|
||||||
|
|
|
@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `.does_not_exist.foo.bar`
|
error: lint:unresolved-import: Cannot resolve imported module `.does_not_exist.foo.bar`
|
||||||
--> src/mdtest_snippet.py:1:7
|
--> src/mdtest_snippet.py:1:7
|
||||||
|
|
|
|
||||||
1 | from .does_not_exist.foo.bar import add # error: [unresolved-import]
|
1 | from .does_not_exist.foo.bar import add # error: [unresolved-import]
|
||||||
|
|
|
@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `does_not_exist`
|
error: lint:unresolved-import: Cannot resolve imported module `does_not_exist`
|
||||||
--> src/mdtest_snippet.py:1:6
|
--> src/mdtest_snippet.py:1:6
|
||||||
|
|
|
|
||||||
1 | from does_not_exist import add # error: [unresolved-import]
|
1 | from does_not_exist import add # error: [unresolved-import]
|
||||||
|
|
|
@ -32,7 +32,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/unresolved_i
|
||||||
# Diagnostics
|
# Diagnostics
|
||||||
|
|
||||||
```
|
```
|
||||||
error: lint:unresolved-import: Cannot resolve import `....foo`
|
error: lint:unresolved-import: Cannot resolve imported module `....foo`
|
||||||
--> src/package/subpackage/subsubpackage/__init__.py:1:10
|
--> src/package/subpackage/subsubpackage/__init__.py:1:10
|
||||||
|
|
|
|
||||||
1 | from ....foo import add # error: [unresolved-import]
|
1 | from ....foo import add # error: [unresolved-import]
|
||||||
|
|
|
@ -3612,7 +3612,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
builder.into_diagnostic(format_args!(
|
builder.into_diagnostic(format_args!(
|
||||||
"Cannot resolve import `{}{}`",
|
"Cannot resolve imported module `{}{}`",
|
||||||
".".repeat(level as usize),
|
".".repeat(level as usize),
|
||||||
module.unwrap_or_default()
|
module.unwrap_or_default()
|
||||||
));
|
));
|
||||||
|
@ -3680,21 +3680,14 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
level: _,
|
level: _,
|
||||||
} = import;
|
} = import;
|
||||||
|
|
||||||
|
self.check_import_from_module_is_resolvable(import);
|
||||||
|
|
||||||
for alias in names {
|
for alias in names {
|
||||||
let definitions = self.index.definitions(alias);
|
for definition in self.index.definitions(alias) {
|
||||||
if definitions.is_empty() {
|
|
||||||
// If the module couldn't be resolved while constructing the semantic index,
|
|
||||||
// this node won't have any definitions associated with it -- but we need to
|
|
||||||
// make sure that we still emit the diagnostic for the unresolvable module,
|
|
||||||
// since this will cause the import to fail at runtime.
|
|
||||||
self.resolve_import_from_module(import, alias);
|
|
||||||
} else {
|
|
||||||
for definition in definitions {
|
|
||||||
self.extend(infer_definition_types(self.db(), *definition));
|
self.extend(infer_definition_types(self.db(), *definition));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn infer_assert_statement(&mut self, assert: &ast::StmtAssert) {
|
fn infer_assert_statement(&mut self, assert: &ast::StmtAssert) {
|
||||||
let ast::StmtAssert {
|
let ast::StmtAssert {
|
||||||
|
@ -3746,11 +3739,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
|
|
||||||
/// Resolve the [`ModuleName`], and the type of the module, being referred to by an
|
/// Resolve the [`ModuleName`], and the type of the module, being referred to by an
|
||||||
/// [`ast::StmtImportFrom`] node. Emit a diagnostic if the module cannot be resolved.
|
/// [`ast::StmtImportFrom`] node. Emit a diagnostic if the module cannot be resolved.
|
||||||
fn resolve_import_from_module(
|
fn check_import_from_module_is_resolvable(&mut self, import_from: &ast::StmtImportFrom) {
|
||||||
&mut self,
|
|
||||||
import_from: &ast::StmtImportFrom,
|
|
||||||
alias: &ast::Alias,
|
|
||||||
) -> Option<(ModuleName, Type<'db>)> {
|
|
||||||
let ast::StmtImportFrom { module, level, .. } = import_from;
|
let ast::StmtImportFrom { module, level, .. } = import_from;
|
||||||
|
|
||||||
// For diagnostics, we want to highlight the unresolvable
|
// For diagnostics, we want to highlight the unresolvable
|
||||||
|
@ -3762,8 +3751,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
let module = module.as_deref();
|
let module = module.as_deref();
|
||||||
|
|
||||||
tracing::trace!(
|
tracing::trace!(
|
||||||
"Resolving imported object `{}` from module `{}` into file `{}`",
|
"Resolving import statement from module `{}` into file `{}`",
|
||||||
alias.name,
|
|
||||||
format_import_from_module(*level, module),
|
format_import_from_module(*level, module),
|
||||||
self.file().path(self.db()),
|
self.file().path(self.db()),
|
||||||
);
|
);
|
||||||
|
@ -3774,7 +3762,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
Err(ModuleNameResolutionError::InvalidSyntax) => {
|
Err(ModuleNameResolutionError::InvalidSyntax) => {
|
||||||
tracing::debug!("Failed to resolve import due to invalid syntax");
|
tracing::debug!("Failed to resolve import due to invalid syntax");
|
||||||
// Invalid syntax diagnostics are emitted elsewhere.
|
// Invalid syntax diagnostics are emitted elsewhere.
|
||||||
return None;
|
return;
|
||||||
}
|
}
|
||||||
Err(ModuleNameResolutionError::TooManyDots) => {
|
Err(ModuleNameResolutionError::TooManyDots) => {
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
|
@ -3787,7 +3775,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
*level,
|
*level,
|
||||||
module,
|
module,
|
||||||
);
|
);
|
||||||
return None;
|
return;
|
||||||
}
|
}
|
||||||
Err(ModuleNameResolutionError::UnknownCurrentModule) => {
|
Err(ModuleNameResolutionError::UnknownCurrentModule) => {
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
|
@ -3801,16 +3789,13 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
*level,
|
*level,
|
||||||
module,
|
module,
|
||||||
);
|
);
|
||||||
return None;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(module_ty) = self.module_type_from_name(&module_name) else {
|
if resolve_module(self.db(), &module_name).is_none() {
|
||||||
self.report_unresolved_import(import_from.into(), module_ref.range(), *level, module);
|
self.report_unresolved_import(import_from.into(), module_ref.range(), *level, module);
|
||||||
return None;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
Some((module_name, module_ty))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_import_from_definition(
|
fn infer_import_from_definition(
|
||||||
|
@ -3819,12 +3804,18 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
alias: &ast::Alias,
|
alias: &ast::Alias,
|
||||||
definition: Definition<'db>,
|
definition: Definition<'db>,
|
||||||
) {
|
) {
|
||||||
let Some((module_name, module_ty)) = self.resolve_import_from_module(import_from, alias)
|
let Ok(module_name) =
|
||||||
|
ModuleName::from_import_statement(self.db(), self.file(), import_from)
|
||||||
else {
|
else {
|
||||||
self.add_unknown_declaration_with_binding(alias.into(), definition);
|
self.add_unknown_declaration_with_binding(alias.into(), definition);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let Some(module_ty) = self.module_type_from_name(&module_name) else {
|
||||||
|
self.add_unknown_declaration_with_binding(alias.into(), definition);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
// The indirection of having `star_import_info` as a separate variable
|
// The indirection of having `star_import_info` as a separate variable
|
||||||
// is required in order to make the borrow checker happy.
|
// is required in order to make the borrow checker happy.
|
||||||
let star_import_info = definition
|
let star_import_info = definition
|
||||||
|
|
|
@ -27,5 +27,8 @@ fn check() {
|
||||||
diagnostic.to_range(&workspace).unwrap().start,
|
diagnostic.to_range(&workspace).unwrap().start,
|
||||||
Position { line: 1, column: 8 }
|
Position { line: 1, column: 8 }
|
||||||
);
|
);
|
||||||
assert_eq!(diagnostic.message(), "Cannot resolve import `random22`");
|
assert_eq!(
|
||||||
|
diagnostic.message(),
|
||||||
|
"Cannot resolve imported module `random22`"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue