Skip to content

Commit 2527125

Browse files
committed
resolve: Bind primitive types to items in libcore
1 parent 1049986 commit 2527125

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+169
-87
lines changed

src/libcollections/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#![feature(pattern)]
4747
#![feature(placement_in)]
4848
#![feature(placement_new_protocol)]
49+
#![cfg_attr(not(stage0), feature(primitive_type))]
4950
#![feature(shared)]
5051
#![feature(slice_patterns)]
5152
#![feature(staged_api)]
@@ -56,7 +57,6 @@
5657
#![feature(unique)]
5758
#![feature(unsafe_no_drop_flag)]
5859
#![cfg_attr(test, feature(rand, test))]
59-
6060
#![no_std]
6161

6262
extern crate rustc_unicode;
@@ -99,6 +99,7 @@ pub mod fmt;
9999
pub mod linked_list;
100100
pub mod range;
101101
pub mod slice;
102+
#[cfg_attr(not(stage0), primitive_type)]
102103
pub mod str;
103104
pub mod string;
104105
pub mod vec;

src/libcore/fmt/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use num::flt2dec;
2121
use ops::Deref;
2222
use result;
2323
use slice;
24-
use str;
2524

2625
#[unstable(feature = "fmt_flags_align", issue = "27726")]
2726
/// Possible alignments returned by `Formatter::align`

src/libcore/fmt/num.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use prelude::v1::*;
1919
use fmt;
2020
use num::Zero;
2121
use ops::{Div, Rem, Sub};
22-
use str;
2322
use slice;
2423
use ptr;
2524
use mem;

src/libcore/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
#![deny(missing_docs)]
5959
#![deny(missing_debug_implementations)]
6060
#![cfg_attr(not(stage0), deny(warnings))]
61+
// This is a temporary way to use libcore's prelude in libcore
62+
#![cfg_attr(not(stage0), feature(primitive_type, local_prelude))]
63+
#![cfg_attr(not(stage0), local_prelude)]
6164

6265
#![feature(allow_internal_unstable)]
6366
#![feature(associated_type_defaults)]
@@ -93,19 +96,43 @@ mod int_macros;
9396
#[macro_use]
9497
mod uint_macros;
9598

99+
/// The boolean type.
100+
#[cfg(not(stage0))]
101+
#[stable(feature = "core_primitive_types", since = "1.9.0")]
102+
#[allow(non_camel_case_types)]
103+
#[primitive_type]
104+
pub type bool = bool;
105+
106+
/// The boolean type.
107+
#[cfg(stage0)]
108+
#[stable(feature = "core_primitive_types", since = "1.9.0")]
109+
pub mod bool {}
110+
111+
#[cfg_attr(not(stage0), primitive_type)]
96112
#[path = "num/isize.rs"] pub mod isize;
113+
#[cfg_attr(not(stage0), primitive_type)]
97114
#[path = "num/i8.rs"] pub mod i8;
115+
#[cfg_attr(not(stage0), primitive_type)]
98116
#[path = "num/i16.rs"] pub mod i16;
117+
#[cfg_attr(not(stage0), primitive_type)]
99118
#[path = "num/i32.rs"] pub mod i32;
119+
#[cfg_attr(not(stage0), primitive_type)]
100120
#[path = "num/i64.rs"] pub mod i64;
101121

122+
#[cfg_attr(not(stage0), primitive_type)]
102123
#[path = "num/usize.rs"] pub mod usize;
124+
#[cfg_attr(not(stage0), primitive_type)]
103125
#[path = "num/u8.rs"] pub mod u8;
126+
#[cfg_attr(not(stage0), primitive_type)]
104127
#[path = "num/u16.rs"] pub mod u16;
128+
#[cfg_attr(not(stage0), primitive_type)]
105129
#[path = "num/u32.rs"] pub mod u32;
130+
#[cfg_attr(not(stage0), primitive_type)]
106131
#[path = "num/u64.rs"] pub mod u64;
107132

133+
#[cfg_attr(not(stage0), primitive_type)]
108134
#[path = "num/f32.rs"] pub mod f32;
135+
#[cfg_attr(not(stage0), primitive_type)]
109136
#[path = "num/f64.rs"] pub mod f64;
110137

111138
#[macro_use]
@@ -138,6 +165,7 @@ pub mod any;
138165
pub mod array;
139166
pub mod sync;
140167
pub mod cell;
168+
#[cfg_attr(not(stage0), primitive_type)]
141169
pub mod char;
142170
pub mod panicking;
143171
pub mod iter;
@@ -146,6 +174,7 @@ pub mod raw;
146174
pub mod result;
147175

148176
pub mod slice;
177+
#[cfg_attr(not(stage0), primitive_type)]
149178
pub mod str;
150179
pub mod hash;
151180
pub mod fmt;

src/libcore/num/dec2flt/rawfp.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
//! take the universally-correct slow path (Algorithm M) for very small and very large numbers.
2929
//! That algorithm needs only next_float() which does handle subnormals and zeros.
3030
use prelude::v1::*;
31-
use u32;
3231
use cmp::Ordering::{Less, Equal, Greater};
3332
use ops::{Mul, Div, Neg};
3433
use fmt::{Debug, LowerExp};

src/libcore/num/flt2dec/decoder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
1313
use prelude::v1::*;
1414

15-
use {f32, f64};
1615
use num::{Float, FpCategory};
1716

1817
/// Decoded unsigned finite value, such that:

src/libcore/num/flt2dec/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ functions.
131131
issue = "0")]
132132

133133
use prelude::v1::*;
134-
use i16;
135134
pub use self::decoder::{decode, DecodableFloat, FullDecoded, Decoded};
136135

137136
pub mod estimator;

src/libcore/prelude/v1.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! same manner as the standard library's prelude.
1616
1717
#![stable(feature = "core_prelude", since = "1.4.0")]
18+
#![cfg_attr(not(stage0), no_implicit_prelude)]
1819

1920
// Reexported core operators
2021
#[stable(feature = "core_prelude", since = "1.4.0")]
@@ -51,3 +52,7 @@
5152
#[doc(no_inline)] pub use str::StrExt;
5253
#[stable(feature = "core_prelude", since = "1.4.0")]
5354
#[doc(no_inline)] pub use char::CharExt;
55+
56+
#[stable(feature = "core_primitive_types", since = "1.9.0")]
57+
#[doc(no_inline)] pub use {u8, u16, u32, u64, usize, i8, i16, i32, i64, isize,
58+
f32, f64, bool, char, str};

src/libcore/str/pattern.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use prelude::v1::*;
2121

2222
use cmp;
2323
use fmt;
24-
use usize;
2524

2625
// Pattern
2726

src/libcoretest/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![feature(libc)]
2929
#![feature(nonzero)]
3030
#![feature(peekable_is_empty)]
31+
#![feature(primitive_type)]
3132
#![feature(ptr_as_ref)]
3233
#![feature(rand)]
3334
#![feature(raw)]

src/libcoretest/num/flt2dec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use std::prelude::v1::*;
12-
use std::{str, mem, i16, f32, f64, fmt};
12+
use std::{mem, fmt};
1313
use std::__rand as rand;
1414
use rand::{Rand, XorShiftRng};
1515
use rand::distributions::{IndependentSample, Range};

src/libcoretest/num/flt2dec/strategy/dragon.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std::prelude::v1::*;
12-
use std::{i16, f64};
1312
use super::super::*;
1413
use core::num::flt2dec::*;
1514
use core::num::bignum::Big32x40 as Big;

src/libcoretest/num/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod uint_macros;
2626

2727
mod u8;
2828
mod u16;
29+
#[primitive_type]
2930
mod u32;
3031
mod u64;
3132

src/librustc/middle/def.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,29 @@ impl Def {
121121
}
122122
}
123123

124-
pub fn def_id(&self) -> DefId {
124+
pub fn opt_def_id(&self) -> Option<DefId> {
125125
match *self {
126126
Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
127127
Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
128128
Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) |
129129
Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
130130
Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
131-
id
131+
Some(id)
132132
}
133133

134134
Def::Label(..) |
135135
Def::PrimTy(..) |
136136
Def::SelfTy(..) |
137137
Def::Err => {
138-
panic!("attempted .def_id() on invalid def: {:?}", self)
138+
None
139139
}
140140
}
141141
}
142142

143+
pub fn def_id(&self) -> DefId {
144+
self.opt_def_id().expect(&format!("attempted .def_id() on invalid def: {:?}", self))
145+
}
146+
143147
pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
144148
match *self {
145149
Def::Variant(enum_id, var_id) => {

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
134134
} else {
135135
DefModifiers::empty()
136136
} | DefModifiers::IMPORTABLE;
137+
if item.attrs.iter().any(|attr| attr.name() == "primitive_type") {
138+
if let Some(def_id) = self.ast_map.opt_local_def_id(item.id) {
139+
self.resolver.primitive_type_items.insert(def_id);
140+
}
141+
}
137142

138143
match item.node {
139144
ItemUse(ref view_path) => {
@@ -463,6 +468,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
463468
if new_parent.is_normal() {
464469
modifiers = modifiers | DefModifiers::IMPORTABLE;
465470
}
471+
if self.session.cstore.item_attrs(def.def_id()).
472+
iter().any(|attr| attr.name() == "primitive_type") {
473+
self.resolver.primitive_type_items.insert(def.def_id());
474+
}
466475

467476
match def {
468477
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {

src/librustc_resolve/lib.rs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use rustc::middle::def_id::DefId;
5757
use rustc::middle::pat_util::pat_bindings;
5858
use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace};
5959
use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap};
60-
use rustc::util::nodemap::{NodeMap, FnvHashMap};
60+
use rustc::util::nodemap::{DefIdSet, NodeMap, FnvHashMap};
6161

6262
use syntax::ast::{self, FloatTy};
6363
use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy};
@@ -1082,6 +1082,7 @@ pub struct Resolver<'a, 'tcx: 'a> {
10821082
freevars_seen: NodeMap<NodeMap<usize>>,
10831083
export_map: ExportMap,
10841084
trait_map: TraitMap,
1085+
primitive_type_items: DefIdSet,
10851086

10861087
// Whether or not to print error messages. Can be set to true
10871088
// when getting additional info for error message suggestions,
@@ -1172,6 +1173,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11721173
trait_map: NodeMap(),
11731174
used_imports: HashSet::new(),
11741175
used_crates: HashSet::new(),
1176+
primitive_type_items: DefIdSet(),
11751177

11761178
emit_errors: true,
11771179
make_glob_map: make_glob_map == MakeGlobMap::Yes,
@@ -2596,14 +2598,38 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25962598
ResolveAttempt(resolution)
25972599
}
25982600

2599-
/// Skips `path_depth` trailing segments, which is also reflected in the
2600-
/// returned value. See `middle::def::PathResolution` for more info.
26012601
pub fn resolve_path(&mut self,
26022602
id: NodeId,
26032603
path: &Path,
26042604
path_depth: usize,
26052605
namespace: Namespace)
26062606
-> Option<PathResolution> {
2607+
let resolution = self.resolve_path_noprim(id, path, path_depth, namespace);
2608+
// Check if the resolution is a #[primitive_type] item in type namespace,
2609+
// replace that item with a primitive type with the same name.
2610+
if let Some(PathResolution{base_def, ..}) = resolution {
2611+
if let Some(def_id) = base_def.opt_def_id() {
2612+
if namespace == TypeNS && self.primitive_type_items.contains(&def_id) {
2613+
let name = &path.segments[path.segments.len() - path_depth - 1]
2614+
.identifier.unhygienic_name;
2615+
if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(name) {
2616+
return Some(PathResolution::new(Def::PrimTy(*prim_ty), path_depth))
2617+
}
2618+
}
2619+
}
2620+
}
2621+
2622+
resolution
2623+
}
2624+
2625+
/// Skips `path_depth` trailing segments, which is also reflected in the
2626+
/// returned value. See `middle::def::PathResolution` for more info.
2627+
pub fn resolve_path_noprim(&mut self,
2628+
id: NodeId,
2629+
path: &Path,
2630+
path_depth: usize,
2631+
namespace: Namespace)
2632+
-> Option<PathResolution> {
26072633
let span = path.span;
26082634
let segments = &path.segments[..path.segments.len() - path_depth];
26092635

@@ -2616,37 +2642,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26162642

26172643
// Try to find a path to an item in a module.
26182644
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-
26322645
if segments.len() == 1 {
2633-
// In `a(::assoc_item)*` `a` cannot be a module. If `a` does resolve to a module we
2634-
// don't report an error right away, but try to fallback to a primitive type.
2635-
// So, we are still able to successfully resolve something like
2636-
//
2637-
// use std::u8; // bring module u8 in scope
2638-
// fn f() -> u8 { // OK, resolves to primitive u8, not to std::u8
2639-
// u8::max_value() // OK, resolves to associated function <u8>::max_value,
2640-
// // not to non-existent std::u8::max_value
2641-
// }
2642-
//
2643-
// Such behavior is required for backward compatibility.
2644-
// The same fallback is used when `a` resolves to nothing.
2645-
let unqualified_def = resolve_identifier_with_fallback(self, true);
2646+
let unqualified_def = self.resolve_identifier(last_ident, namespace, true);
26462647
return unqualified_def.and_then(|def| self.adjust_local_def(def, span)).map(mk_res);
26472648
}
26482649

2649-
let unqualified_def = resolve_identifier_with_fallback(self, false);
2650+
let unqualified_def = self.resolve_identifier(last_ident, namespace, false);
26502651
let def = self.resolve_module_relative_path(span, segments, namespace);
26512652
match (def, unqualified_def) {
26522653
(Some(d), Some(ref ud)) if d == ud.def => {
@@ -2932,17 +2933,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29322933
span: Span,
29332934
name_path: &[ast::Name])
29342935
-> Option<Module<'a>> {
2935-
let last_name = name_path.last().unwrap();
2936-
2937-
if name_path.len() == 1 {
2938-
match this.primitive_type_table.primitive_types.get(last_name) {
2939-
Some(_) => None,
2940-
None => this.current_module.resolve_name(*last_name, TypeNS, true).success()
2941-
.and_then(NameBinding::module)
2942-
}
2943-
} else {
2944-
this.resolve_module_path(&name_path, UseLexicalScope, span).success()
2945-
}
2936+
this.resolve_module_path(&name_path, UseLexicalScope, span).success()
29462937
}
29472938

29482939
fn is_static_method(this: &Resolver, did: DefId) -> bool {

src/librustc_unicode/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434

3535
#![feature(core_char_ext)]
3636
#![feature(lang_items)]
37+
#![cfg_attr(not(stage0), feature(primitive_type))]
3738
#![feature(staged_api)]
3839

3940
mod tables;
4041
mod u_str;
42+
#[cfg_attr(not(stage0), primitive_type)]
4143
pub mod char;
4244

4345
#[allow(deprecated)]

src/libstd/fs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,6 @@ mod tests {
14611461
use io::{ErrorKind, SeekFrom};
14621462
use path::Path;
14631463
use rand::{StdRng, Rng};
1464-
use str;
14651464
use sys_common::io::test::{TempDir, tmpdir};
14661465

14671466
#[cfg(windows)]

0 commit comments

Comments
 (0)