Skip to content

Commit f880acd

Browse files
authored
Merge pull request #19461 from Hmikihiro/shadow_by_module
fix: shadow type by module
2 parents 062205f + 2bdb229 commit f880acd

File tree

6 files changed

+180
-7
lines changed

6 files changed

+180
-7
lines changed

crates/hir-def/src/resolver.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ pub enum TypeNs {
105105
BuiltinType(BuiltinType),
106106
TraitId(TraitId),
107107
TraitAliasId(TraitAliasId),
108-
// Module belong to type ns, but the resolver is used when all module paths
109-
// are fully resolved.
110-
// ModuleId(ModuleId)
108+
109+
ModuleId(ModuleId),
111110
}
112111

113112
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -249,6 +248,24 @@ impl Resolver {
249248
}
250249
Scope::BlockScope(m) => {
251250
if let Some(res) = m.resolve_path_in_type_ns(db, path) {
251+
let res = match res.0 {
252+
TypeNs::ModuleId(_) if res.1.is_none() => {
253+
if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE
254+
.get(first_name)
255+
.and_then(|builtin| builtin.take_types())
256+
{
257+
(
258+
TypeNs::BuiltinType(builtin),
259+
remaining_idx(),
260+
None,
261+
ResolvePathResultPrefixInfo::default(),
262+
)
263+
} else {
264+
res
265+
}
266+
}
267+
_ => res,
268+
};
252269
return Some(res);
253270
}
254271
}
@@ -1193,11 +1210,12 @@ fn to_type_ns(per_ns: PerNs) -> Option<(TypeNs, Option<ImportOrExternCrate>)> {
11931210
ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
11941211
ModuleDefId::TraitAliasId(it) => TypeNs::TraitAliasId(it),
11951212

1213+
ModuleDefId::ModuleId(it) => TypeNs::ModuleId(it),
1214+
11961215
ModuleDefId::FunctionId(_)
11971216
| ModuleDefId::ConstId(_)
11981217
| ModuleDefId::MacroId(_)
1199-
| ModuleDefId::StaticId(_)
1200-
| ModuleDefId::ModuleId(_) => return None,
1218+
| ModuleDefId::StaticId(_) => return None,
12011219
};
12021220
Some((res, def.import))
12031221
}

crates/hir-ty/src/infer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,8 @@ impl<'a> InferenceContext<'a> {
16661666
TypeNs::AdtId(AdtId::EnumId(_))
16671667
| TypeNs::BuiltinType(_)
16681668
| TypeNs::TraitId(_)
1669-
| TypeNs::TraitAliasId(_) => {
1669+
| TypeNs::TraitAliasId(_)
1670+
| TypeNs::ModuleId(_) => {
16701671
// FIXME diagnostic
16711672
(self.err_ty(), None)
16721673
}

crates/hir-ty/src/lower/path.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
279279
TypeNs::BuiltinType(it) => self.lower_path_inner(it.into(), infer_args),
280280
TypeNs::TypeAliasId(it) => self.lower_path_inner(it.into(), infer_args),
281281
// FIXME: report error
282-
TypeNs::EnumVariantId(_) => return (TyKind::Error.intern(Interner), None),
282+
TypeNs::EnumVariantId(_) | TypeNs::ModuleId(_) => {
283+
return (TyKind::Error.intern(Interner), None);
284+
}
283285
};
284286

285287
self.skip_resolved_segment();
@@ -310,6 +312,9 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
310312
TypeNs::BuiltinType(_) => {
311313
prohibit_generics_on_resolved(GenericArgsProhibitedReason::PrimitiveTy)
312314
}
315+
TypeNs::ModuleId(_) => {
316+
prohibit_generics_on_resolved(GenericArgsProhibitedReason::Module)
317+
}
313318
TypeNs::AdtId(_)
314319
| TypeNs::EnumVariantId(_)
315320
| TypeNs::TypeAliasId(_)

crates/hir/src/attrs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ fn resolve_assoc_or_field(
210210
// XXX: Do these get resolved?
211211
return None;
212212
}
213+
TypeNs::ModuleId(_) => {
214+
return None;
215+
}
213216
};
214217

215218
// Resolve inherent items first, then trait items, then fields.

crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ fn resolve_hir_path_(
15271527
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
15281528
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
15291529
TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()),
1530+
TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())),
15301531
};
15311532
match unresolved {
15321533
Some(unresolved) => resolver
@@ -1654,6 +1655,7 @@ fn resolve_hir_path_qualifier(
16541655
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
16551656
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
16561657
TypeNs::TraitAliasId(it) => PathResolution::Def(TraitAlias::from(it).into()),
1658+
TypeNs::ModuleId(it) => PathResolution::Def(ModuleDef::Module(it.into())),
16571659
};
16581660
match unresolved {
16591661
Some(unresolved) => resolver

crates/ide/src/goto_definition.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,6 +3330,150 @@ pub fn foo() {}
33303330
fn main() {
33313331
let s = st$0r::f();
33323332
}
3333+
"#,
3334+
);
3335+
}
3336+
3337+
#[test]
3338+
fn struct_shadow_by_module() {
3339+
check(
3340+
r#"
3341+
mod foo {
3342+
pub mod bar {
3343+
// ^^^
3344+
pub type baz = usize;
3345+
}
3346+
}
3347+
struct bar;
3348+
fn main() {
3349+
use foo::bar;
3350+
let x: ba$0r::baz = 5;
3351+
3352+
}
3353+
"#,
3354+
);
3355+
}
3356+
3357+
#[test]
3358+
fn type_alias_shadow_by_module() {
3359+
check(
3360+
r#"
3361+
mod foo {
3362+
pub mod bar {
3363+
// ^^^
3364+
pub fn baz() {}
3365+
}
3366+
}
3367+
3368+
trait Qux {}
3369+
3370+
fn item<bar: Qux>() {
3371+
use foo::bar;
3372+
ba$0r::baz();
3373+
}
3374+
}
3375+
"#,
3376+
);
3377+
3378+
check(
3379+
r#"
3380+
mod foo {
3381+
pub mod bar {
3382+
// ^^^
3383+
pub fn baz() {}
3384+
}
3385+
}
3386+
3387+
fn item<bar>(x: bar) {
3388+
use foo::bar;
3389+
let x: bar$0 = x;
3390+
}
3391+
"#,
3392+
);
3393+
}
3394+
3395+
#[test]
3396+
fn trait_shadow_by_module() {
3397+
check(
3398+
r#"
3399+
pub mod foo {
3400+
pub mod Bar {}
3401+
// ^^^
3402+
}
3403+
3404+
trait Bar {}
3405+
3406+
fn main() {
3407+
use foo::Bar;
3408+
fn f<Qux: B$0ar>() {}
3409+
}
3410+
"#,
3411+
);
3412+
}
3413+
3414+
#[test]
3415+
fn const_shadow_by_module() {
3416+
check(
3417+
r#"
3418+
pub mod foo {
3419+
pub struct u8 {}
3420+
pub mod bar {
3421+
pub mod u8 {}
3422+
}
3423+
}
3424+
3425+
fn main() {
3426+
use foo::u8;
3427+
{
3428+
use foo::bar::u8;
3429+
3430+
fn f1<const N: u$08>() {}
3431+
}
3432+
fn f2<const N: u8>() {}
3433+
}
3434+
"#,
3435+
);
3436+
3437+
check(
3438+
r#"
3439+
pub mod foo {
3440+
pub struct u8 {}
3441+
// ^^
3442+
pub mod bar {
3443+
pub mod u8 {}
3444+
}
3445+
}
3446+
3447+
fn main() {
3448+
use foo::u8;
3449+
{
3450+
use foo::bar::u8;
3451+
3452+
fn f1<const N: u8>() {}
3453+
}
3454+
fn f2<const N: u$08>() {}
3455+
}
3456+
"#,
3457+
);
3458+
3459+
check(
3460+
r#"
3461+
pub mod foo {
3462+
pub struct buz {}
3463+
pub mod bar {
3464+
pub mod buz {}
3465+
// ^^^
3466+
}
3467+
}
3468+
3469+
fn main() {
3470+
use foo::buz;
3471+
{
3472+
use foo::bar::buz;
3473+
3474+
fn f1<const N: buz$0>() {}
3475+
}
3476+
}
33333477
"#,
33343478
);
33353479
}

0 commit comments

Comments
 (0)