Skip to content

fix: shadow type by module #19461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions crates/hir-def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ pub enum TypeNs {
BuiltinType(BuiltinType),
TraitId(TraitId),
TraitAliasId(TraitAliasId),
// Module belong to type ns, but the resolver is used when all module paths
// are fully resolved.
// ModuleId(ModuleId)

ModuleId(ModuleId),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -247,6 +246,24 @@ impl Resolver {
}
Scope::BlockScope(m) => {
if let Some(res) = m.resolve_path_in_type_ns(db, path) {
let res = match res.0 {
TypeNs::ModuleId(_) if res.1.is_none() => {
if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE
.get(first_name)
.and_then(|builtin| builtin.take_types())
{
(
TypeNs::BuiltinType(builtin),
remaining_idx(),
None,
ResolvePathResultPrefixInfo::default(),
)
} else {
res
}
}
_ => res,
};
return Some(res);
}
}
Expand Down Expand Up @@ -1204,11 +1221,12 @@ fn to_type_ns(per_ns: PerNs) -> Option<(TypeNs, Option<ImportOrExternCrate>)> {
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
ModuleDefId::TraitAliasId(it) => TypeNs::TraitAliasId(it),

ModuleDefId::ModuleId(it) => TypeNs::ModuleId(it),

ModuleDefId::FunctionId(_)
| ModuleDefId::ConstId(_)
| ModuleDefId::MacroId(_)
| ModuleDefId::StaticId(_)
| ModuleDefId::ModuleId(_) => return None,
| ModuleDefId::StaticId(_) => return None,
};
Some((res, def.import))
}
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,8 @@ impl<'a> InferenceContext<'a> {
TypeNs::AdtId(AdtId::EnumId(_))
| TypeNs::BuiltinType(_)
| TypeNs::TraitId(_)
| TypeNs::TraitAliasId(_) => {
| TypeNs::TraitAliasId(_)
| TypeNs::ModuleId(_) => {
// FIXME diagnostic
(self.err_ty(), None)
Comment on lines +1652 to 1654
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mod Foo {}
fn main() {
    Foo {}
 // ^^^ not a struct, variant or union type
}

Modules do not have variants, so this results in an error.

}
Expand Down
7 changes: 6 additions & 1 deletion crates/hir-ty/src/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
TypeNs::BuiltinType(it) => self.lower_path_inner(it.into(), infer_args),
TypeNs::TypeAliasId(it) => self.lower_path_inner(it.into(), infer_args),
// FIXME: report error
TypeNs::EnumVariantId(_) => return (TyKind::Error.intern(Interner), None),
TypeNs::EnumVariantId(_) | TypeNs::ModuleId(_) => {
return (TyKind::Error.intern(Interner), None);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub mod M {
    pub mod Module {}
}

fn sample_function<Module>(x: Module) {
    let x: Module = x;
    {
        use M::Module;
        let x: Module = x;
            // ^^^^^^ not a type
    }
}

fn main() {}

This kind of pattern is similar to the above code.
Here, Module refers to a module, not a type, so using it as a type results in an error.
That's why we return TyKind::Error for TypeNs::ModuleId.

}
};

self.skip_resolved_segment();
Expand Down Expand Up @@ -316,6 +318,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
TypeNs::BuiltinType(_) => {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::PrimitiveTy)
}
TypeNs::ModuleId(_) => {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::Module)
}
TypeNs::AdtId(_)
| TypeNs::EnumVariantId(_)
| TypeNs::TypeAliasId(_)
Expand Down
3 changes: 3 additions & 0 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ fn resolve_assoc_or_field(
// XXX: Do these get resolved?
return None;
}
TypeNs::ModuleId(_) => {
return None;
}
};

// Resolve inherent items first, then trait items, then fields.
Expand Down
2 changes: 2 additions & 0 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ fn resolve_hir_path_(
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()),
TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())),
};
match unresolved {
Some(unresolved) => resolver
Expand Down Expand Up @@ -1555,6 +1556,7 @@ fn resolve_hir_path_qualifier(
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()),
TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())),
};
match unresolved {
Some(unresolved) => resolver
Expand Down
144 changes: 144 additions & 0 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,150 @@ pub fn foo() {}
fn main() {
let s = st$0r::f();
}
"#,
);
}

#[test]
fn struct_shadow_by_module() {
check(
r#"
mod foo {
pub mod bar {
// ^^^
pub type baz = usize;
}
}
struct bar;
fn main() {
use foo::bar;
let x: ba$0r::baz = 5;

}
"#,
);
}

#[test]
fn type_alias_shadow_by_module() {
check(
r#"
mod foo {
pub mod bar {
// ^^^
pub fn baz() {}
}
}

trait Qux {}

fn item<bar: Qux>() {
use foo::bar;
ba$0r::baz();
}
}
"#,
);

check(
r#"
mod foo {
pub mod bar {
// ^^^
pub fn baz() {}
}
}

fn item<bar>(x: bar) {
use foo::bar;
let x: bar$0 = x;
}
"#,
);
}

#[test]
fn trait_shadow_by_module() {
check(
r#"
pub mod foo {
pub mod Bar {}
// ^^^
}

trait Bar {}

fn main() {
use foo::Bar;
fn f<Qux: B$0ar>() {}
}
"#,
);
}

#[test]
fn const_shadow_by_module() {
check(
r#"
pub mod foo {
pub struct u8 {}
pub mod bar {
pub mod u8 {}
}
}

fn main() {
use foo::u8;
{
use foo::bar::u8;

fn f1<const N: u$08>() {}
}
fn f2<const N: u8>() {}
}
"#,
);

check(
r#"
pub mod foo {
pub struct u8 {}
// ^^
pub mod bar {
pub mod u8 {}
}
}

fn main() {
use foo::u8;
{
use foo::bar::u8;

fn f1<const N: u8>() {}
}
fn f2<const N: u$08>() {}
}
"#,
);

check(
r#"
pub mod foo {
pub struct buz {}
pub mod bar {
pub mod buz {}
// ^^^
}
}

fn main() {
use foo::buz;
{
use foo::bar::buz;

fn f1<const N: buz$0>() {}
}
}
"#,
);
}
Expand Down