Skip to content

Commit d764e13

Browse files
author
Jonas Schievink
committed
Fallback to primitive when path doesn't resolve
1 parent 63573d4 commit d764e13

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

crates/hir_def/src/builtin_type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl BuiltinType {
6868
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
6969
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
7070
];
71+
72+
pub fn by_name(name: &Name) -> Option<Self> {
73+
Self::ALL.iter().find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
74+
}
7175
}
7276

7377
impl AsName for BuiltinType {

crates/hir_def/src/resolver.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,18 @@ impl Resolver {
317317
}
318318
}
319319

320+
// If a path of the shape `u16::from_le_bytes` failed to resolve at all, then we fall back
321+
// to resolving to the primitive type, to allow this to still work in the presence of
322+
// `use core::u16;`.
323+
if path.kind == PathKind::Plain && path.segments().len() > 1 {
324+
match BuiltinType::by_name(&path.segments()[0]) {
325+
Some(builtin) => {
326+
return Some(ResolveValueResult::Partial(TypeNs::BuiltinType(builtin), 1));
327+
}
328+
None => {}
329+
}
330+
}
331+
320332
None
321333
}
322334

crates/hir_ty/src/tests/method_resolution.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,3 +1767,26 @@ fn foo() {
17671767
"#,
17681768
);
17691769
}
1770+
1771+
#[test]
1772+
fn primitive_assoc_fn_shadowed_by_use() {
1773+
check_types(
1774+
r#"
1775+
//- /lib.rs crate:lib deps:core
1776+
use core::u16;
1777+
1778+
fn f() -> u16 {
1779+
let x = u16::from_le_bytes();
1780+
x
1781+
//^ u16
1782+
}
1783+
1784+
//- /core.rs crate:core
1785+
pub mod u16 {}
1786+
1787+
impl u16 {
1788+
pub fn from_le_bytes() -> Self { 0 }
1789+
}
1790+
"#,
1791+
)
1792+
}

0 commit comments

Comments
 (0)