Skip to content

Commit b418cd2

Browse files
committed
Cleanup
+ Fix a comment and add a test based on it
1 parent 77f033b commit b418cd2

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

Diff for: src/librustc_resolve/lib.rs

+18-30
Original file line numberDiff line numberDiff line change
@@ -2616,27 +2616,37 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26162616

26172617
// Try to find a path to an item in a module.
26182618
let last_ident = segments.last().unwrap().identifier;
2619+
// Resolve a single identifier with fallback to primitive types
2620+
let resolve_identifier_with_fallback = |this: &mut Self, record_used| {
2621+
let def = this.resolve_identifier(last_ident, namespace, record_used);
2622+
match def {
2623+
None | Some(LocalDef{def: Def::Mod(..), ..}) if namespace == TypeNS =>
2624+
this.primitive_type_table
2625+
.primitive_types
2626+
.get(&last_ident.unhygienic_name)
2627+
.map_or(def, |prim_ty| Some(LocalDef::from_def(Def::PrimTy(*prim_ty)))),
2628+
_ => def
2629+
}
2630+
};
2631+
26192632
if segments.len() == 1 {
26202633
// In `a(::assoc_item)*` `a` cannot be a module. If `a` does resolve to a module we
26212634
// don't report an error right away, but try to fallback to a primitive type.
26222635
// So, we are still able to successfully resolve something like
26232636
//
26242637
// use std::u8; // bring module u8 in scope
26252638
// fn f() -> u8 { // OK, resolves to primitive u8, not to std::u8
2626-
// u8::MAX // OK, resolves to associated constant <u8>::MAX,
2627-
// // not to non-existent std::u8::MAX
2639+
// u8::max_value() // OK, resolves to associated function <u8>::max_value,
2640+
// // not to non-existent std::u8::max_value
26282641
// }
26292642
//
26302643
// Such behavior is required for backward compatibility.
26312644
// The same fallback is used when `a` resolves to nothing.
2632-
let unqualified_def = self.resolve_identifier_with_fallback(last_ident, namespace, true);
2633-
return unqualified_def.and_then(|def| self.adjust_local_def(def, span))
2634-
.map(|def| {
2635-
PathResolution::new(def, path_depth)
2636-
});
2645+
let unqualified_def = resolve_identifier_with_fallback(self, true);
2646+
return unqualified_def.and_then(|def| self.adjust_local_def(def, span)).map(mk_res);
26372647
}
26382648

2639-
let unqualified_def = self.resolve_identifier_with_fallback(last_ident, namespace, false);
2649+
let unqualified_def = resolve_identifier_with_fallback(self, false);
26402650
let def = self.resolve_module_relative_path(span, segments, namespace);
26412651
match (def, unqualified_def) {
26422652
(Some(d), Some(ref ud)) if d == ud.def => {
@@ -2652,28 +2662,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26522662
def.map(mk_res)
26532663
}
26542664

2655-
// Resolve a single identifier with fallback to primitive types
2656-
fn resolve_identifier_with_fallback(&mut self,
2657-
identifier: hir::Ident,
2658-
namespace: Namespace,
2659-
check_ribs: bool,
2660-
record_used: bool)
2661-
-> Option<LocalDef> {
2662-
let def = self.resolve_identifier(identifier, namespace, check_ribs, record_used);
2663-
match def {
2664-
None | Some(LocalDef{def: Def::Mod(..), ..}) if namespace == TypeNS => {
2665-
if let Some(&prim_ty) = self.primitive_type_table
2666-
.primitive_types
2667-
.get(&identifier.unhygienic_name) {
2668-
Some(LocalDef::from_def(Def::PrimTy(prim_ty)))
2669-
} else {
2670-
def
2671-
}
2672-
}
2673-
_ => def
2674-
}
2675-
}
2676-
26772665
// Resolve a single identifier
26782666
fn resolve_identifier(&mut self,
26792667
identifier: hir::Ident,

Diff for: src/test/run-pass/issue-20427.rs

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// except according to those terms.
1010

1111
// aux-build:i8.rs
12+
// ignore-pretty (#23623)
13+
1214
extern crate i8;
1315
use std::string as i16;
1416
static i32: i32 = 0;
@@ -66,6 +68,17 @@ mod reuse {
6668
}
6769
}
6870

71+
mod guard {
72+
pub fn check() {
73+
use std::u8; // bring module u8 in scope
74+
fn f() -> u8 { // OK, resolves to primitive u8, not to std::u8
75+
u8::max_value() // OK, resolves to associated function <u8>::max_value,
76+
// not to non-existent std::u8::max_value
77+
}
78+
assert_eq!(f(), u8::MAX); // OK, resolves to std::u8::MAX
79+
}
80+
}
81+
6982
fn main() {
7083
let bool = true;
7184
let _ = match bool {
@@ -74,4 +87,5 @@ fn main() {
7487
};
7588

7689
reuse::check::<u64>();
90+
guard::check();
7791
}

0 commit comments

Comments
 (0)