From 25271258814aaee3d37a9a52deffc7cc356da1f1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 16 Mar 2016 01:10:51 +0300 Subject: [PATCH] resolve: Bind primitive types to items in libcore --- src/libcollections/lib.rs | 3 +- src/libcore/fmt/mod.rs | 1 - src/libcore/fmt/num.rs | 1 - src/libcore/lib.rs | 29 ++++++++ src/libcore/num/dec2flt/rawfp.rs | 1 - src/libcore/num/flt2dec/decoder.rs | 1 - src/libcore/num/flt2dec/mod.rs | 1 - src/libcore/prelude/v1.rs | 5 ++ src/libcore/str/pattern.rs | 1 - src/libcoretest/lib.rs | 1 + src/libcoretest/num/flt2dec/mod.rs | 2 +- .../num/flt2dec/strategy/dragon.rs | 1 - src/libcoretest/num/mod.rs | 1 + src/librustc/middle/def.rs | 10 ++- src/librustc_resolve/build_reduced_graph.rs | 9 +++ src/librustc_resolve/lib.rs | 73 ++++++++----------- src/librustc_unicode/lib.rs | 2 + src/libstd/fs.rs | 1 - src/libstd/lib.rs | 6 ++ src/libstd/prelude/v1.rs | 5 ++ src/libstd/process.rs | 2 - src/libstd/sync/condvar.rs | 1 - src/libstd/sys/windows/handle.rs | 1 - src/libstd/sys/windows/stdio.rs | 1 - src/libstd/thread/mod.rs | 1 - src/libsyntax/feature_gate.rs | 7 ++ src/libsyntax/std_inject.rs | 13 +++- .../associated-types-issue-20346.rs | 1 + src/test/compile-fail/import-shadow-1.rs | 4 +- src/test/compile-fail/import-shadow-2.rs | 4 +- src/test/compile-fail/import-shadow-3.rs | 4 +- src/test/compile-fail/import-shadow-4.rs | 4 +- src/test/compile-fail/import-shadow-5.rs | 4 +- src/test/compile-fail/import-shadow-6.rs | 4 +- src/test/compile-fail/import-shadow-7.rs | 4 +- src/test/compile-fail/issue-19660.rs | 4 +- src/test/compile-fail/privacy1.rs | 9 ++- src/test/compile-fail/privacy2.rs | 5 +- src/test/compile-fail/privacy3.rs | 5 +- src/test/compile-fail/privacy4.rs | 4 +- .../tag-that-dare-not-speak-its-name.rs | 1 + src/test/run-make/simd-ffi/simd.rs | 4 +- src/test/run-make/target-specs/foo.rs | 5 +- src/test/run-pass/num-wrapping.rs | 3 +- src/test/run-pass/smallest-hello-world.rs | 5 +- src/test/run-pass/use.rs | 2 +- 46 files changed, 169 insertions(+), 87 deletions(-) diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index a6be81c91bbf1..1077e0b55b4de 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -46,6 +46,7 @@ #![feature(pattern)] #![feature(placement_in)] #![feature(placement_new_protocol)] +#![cfg_attr(not(stage0), feature(primitive_type))] #![feature(shared)] #![feature(slice_patterns)] #![feature(staged_api)] @@ -56,7 +57,6 @@ #![feature(unique)] #![feature(unsafe_no_drop_flag)] #![cfg_attr(test, feature(rand, test))] - #![no_std] extern crate rustc_unicode; @@ -99,6 +99,7 @@ pub mod fmt; pub mod linked_list; pub mod range; pub mod slice; +#[cfg_attr(not(stage0), primitive_type)] pub mod str; pub mod string; pub mod vec; diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index d2da16624cabe..6d5eb717a2e29 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -21,7 +21,6 @@ use num::flt2dec; use ops::Deref; use result; use slice; -use str; #[unstable(feature = "fmt_flags_align", issue = "27726")] /// Possible alignments returned by `Formatter::align` diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index a944c996c1a1e..ec4bcb6192682 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -19,7 +19,6 @@ use prelude::v1::*; use fmt; use num::Zero; use ops::{Div, Rem, Sub}; -use str; use slice; use ptr; use mem; diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index f199909dfa90a..854711421e3fa 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -58,6 +58,9 @@ #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![cfg_attr(not(stage0), deny(warnings))] +// This is a temporary way to use libcore's prelude in libcore +#![cfg_attr(not(stage0), feature(primitive_type, local_prelude))] +#![cfg_attr(not(stage0), local_prelude)] #![feature(allow_internal_unstable)] #![feature(associated_type_defaults)] @@ -93,19 +96,43 @@ mod int_macros; #[macro_use] mod uint_macros; +/// The boolean type. +#[cfg(not(stage0))] +#[stable(feature = "core_primitive_types", since = "1.9.0")] +#[allow(non_camel_case_types)] +#[primitive_type] +pub type bool = bool; + +/// The boolean type. +#[cfg(stage0)] +#[stable(feature = "core_primitive_types", since = "1.9.0")] +pub mod bool {} + +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/isize.rs"] pub mod isize; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/i8.rs"] pub mod i8; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/i16.rs"] pub mod i16; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/i32.rs"] pub mod i32; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/i64.rs"] pub mod i64; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/usize.rs"] pub mod usize; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/u8.rs"] pub mod u8; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/u16.rs"] pub mod u16; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/u32.rs"] pub mod u32; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/u64.rs"] pub mod u64; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/f32.rs"] pub mod f32; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/f64.rs"] pub mod f64; #[macro_use] @@ -138,6 +165,7 @@ pub mod any; pub mod array; pub mod sync; pub mod cell; +#[cfg_attr(not(stage0), primitive_type)] pub mod char; pub mod panicking; pub mod iter; @@ -146,6 +174,7 @@ pub mod raw; pub mod result; pub mod slice; +#[cfg_attr(not(stage0), primitive_type)] pub mod str; pub mod hash; pub mod fmt; diff --git a/src/libcore/num/dec2flt/rawfp.rs b/src/libcore/num/dec2flt/rawfp.rs index 2099c6a7baa76..356dbdc8c42d9 100644 --- a/src/libcore/num/dec2flt/rawfp.rs +++ b/src/libcore/num/dec2flt/rawfp.rs @@ -28,7 +28,6 @@ //! take the universally-correct slow path (Algorithm M) for very small and very large numbers. //! That algorithm needs only next_float() which does handle subnormals and zeros. use prelude::v1::*; -use u32; use cmp::Ordering::{Less, Equal, Greater}; use ops::{Mul, Div, Neg}; use fmt::{Debug, LowerExp}; diff --git a/src/libcore/num/flt2dec/decoder.rs b/src/libcore/num/flt2dec/decoder.rs index 6265691bde9e9..dca0a3a75131b 100644 --- a/src/libcore/num/flt2dec/decoder.rs +++ b/src/libcore/num/flt2dec/decoder.rs @@ -12,7 +12,6 @@ use prelude::v1::*; -use {f32, f64}; use num::{Float, FpCategory}; /// Decoded unsigned finite value, such that: diff --git a/src/libcore/num/flt2dec/mod.rs b/src/libcore/num/flt2dec/mod.rs index b549f33424264..acab17a149065 100644 --- a/src/libcore/num/flt2dec/mod.rs +++ b/src/libcore/num/flt2dec/mod.rs @@ -131,7 +131,6 @@ functions. issue = "0")] use prelude::v1::*; -use i16; pub use self::decoder::{decode, DecodableFloat, FullDecoded, Decoded}; pub mod estimator; diff --git a/src/libcore/prelude/v1.rs b/src/libcore/prelude/v1.rs index 75db6fceab9b7..90df1d71e6efc 100644 --- a/src/libcore/prelude/v1.rs +++ b/src/libcore/prelude/v1.rs @@ -15,6 +15,7 @@ //! same manner as the standard library's prelude. #![stable(feature = "core_prelude", since = "1.4.0")] +#![cfg_attr(not(stage0), no_implicit_prelude)] // Reexported core operators #[stable(feature = "core_prelude", since = "1.4.0")] @@ -51,3 +52,7 @@ #[doc(no_inline)] pub use str::StrExt; #[stable(feature = "core_prelude", since = "1.4.0")] #[doc(no_inline)] pub use char::CharExt; + +#[stable(feature = "core_primitive_types", since = "1.9.0")] +#[doc(no_inline)] pub use {u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, + f32, f64, bool, char, str}; diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs index b803539e12b1a..ab76756cbf2ee 100644 --- a/src/libcore/str/pattern.rs +++ b/src/libcore/str/pattern.rs @@ -21,7 +21,6 @@ use prelude::v1::*; use cmp; use fmt; -use usize; // Pattern diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 506f9e9b7a576..35ad3d012f8dd 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -28,6 +28,7 @@ #![feature(libc)] #![feature(nonzero)] #![feature(peekable_is_empty)] +#![feature(primitive_type)] #![feature(ptr_as_ref)] #![feature(rand)] #![feature(raw)] diff --git a/src/libcoretest/num/flt2dec/mod.rs b/src/libcoretest/num/flt2dec/mod.rs index 1a592f3ad4249..d5f5da071875f 100644 --- a/src/libcoretest/num/flt2dec/mod.rs +++ b/src/libcoretest/num/flt2dec/mod.rs @@ -9,7 +9,7 @@ // except according to those terms. use std::prelude::v1::*; -use std::{str, mem, i16, f32, f64, fmt}; +use std::{mem, fmt}; use std::__rand as rand; use rand::{Rand, XorShiftRng}; use rand::distributions::{IndependentSample, Range}; diff --git a/src/libcoretest/num/flt2dec/strategy/dragon.rs b/src/libcoretest/num/flt2dec/strategy/dragon.rs index 79dcca7671a2d..b9708d2fc3c11 100644 --- a/src/libcoretest/num/flt2dec/strategy/dragon.rs +++ b/src/libcoretest/num/flt2dec/strategy/dragon.rs @@ -9,7 +9,6 @@ // except according to those terms. use std::prelude::v1::*; -use std::{i16, f64}; use super::super::*; use core::num::flt2dec::*; use core::num::bignum::Big32x40 as Big; diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index 11c1bd667fb36..f1908b9aafb12 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -26,6 +26,7 @@ mod uint_macros; mod u8; mod u16; +#[primitive_type] mod u32; mod u64; diff --git a/src/librustc/middle/def.rs b/src/librustc/middle/def.rs index 6d4799749b93a..cc31937b98ffd 100644 --- a/src/librustc/middle/def.rs +++ b/src/librustc/middle/def.rs @@ -121,25 +121,29 @@ impl Def { } } - pub fn def_id(&self) -> DefId { + pub fn opt_def_id(&self) -> Option { match *self { Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) | Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) | Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) | Def::Local(id, _) | Def::Upvar(id, _, _, _) => { - id + Some(id) } Def::Label(..) | Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => { - panic!("attempted .def_id() on invalid def: {:?}", self) + None } } } + pub fn def_id(&self) -> DefId { + self.opt_def_id().expect(&format!("attempted .def_id() on invalid def: {:?}", self)) + } + pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> { match *self { Def::Variant(enum_id, var_id) => { diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 08b5e51729013..b5ddeb9cfcad4 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -134,6 +134,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { } else { DefModifiers::empty() } | DefModifiers::IMPORTABLE; + if item.attrs.iter().any(|attr| attr.name() == "primitive_type") { + if let Some(def_id) = self.ast_map.opt_local_def_id(item.id) { + self.resolver.primitive_type_items.insert(def_id); + } + } match item.node { ItemUse(ref view_path) => { @@ -463,6 +468,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { if new_parent.is_normal() { modifiers = modifiers | DefModifiers::IMPORTABLE; } + if self.session.cstore.item_attrs(def.def_id()). + iter().any(|attr| attr.name() == "primitive_type") { + self.resolver.primitive_type_items.insert(def.def_id()); + } match def { Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => { diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 7220953d8bcdd..f4ab2b7adc0cd 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -57,7 +57,7 @@ use rustc::middle::def_id::DefId; use rustc::middle::pat_util::pat_bindings; use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace}; use rustc::middle::ty::{Freevar, FreevarMap, TraitMap, GlobMap}; -use rustc::util::nodemap::{NodeMap, FnvHashMap}; +use rustc::util::nodemap::{DefIdSet, NodeMap, FnvHashMap}; use syntax::ast::{self, FloatTy}; use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy}; @@ -1082,6 +1082,7 @@ pub struct Resolver<'a, 'tcx: 'a> { freevars_seen: NodeMap>, export_map: ExportMap, trait_map: TraitMap, + primitive_type_items: DefIdSet, // Whether or not to print error messages. Can be set to true // when getting additional info for error message suggestions, @@ -1172,6 +1173,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { trait_map: NodeMap(), used_imports: HashSet::new(), used_crates: HashSet::new(), + primitive_type_items: DefIdSet(), emit_errors: true, make_glob_map: make_glob_map == MakeGlobMap::Yes, @@ -2596,14 +2598,38 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { ResolveAttempt(resolution) } - /// Skips `path_depth` trailing segments, which is also reflected in the - /// returned value. See `middle::def::PathResolution` for more info. pub fn resolve_path(&mut self, id: NodeId, path: &Path, path_depth: usize, namespace: Namespace) -> Option { + let resolution = self.resolve_path_noprim(id, path, path_depth, namespace); + // Check if the resolution is a #[primitive_type] item in type namespace, + // replace that item with a primitive type with the same name. + if let Some(PathResolution{base_def, ..}) = resolution { + if let Some(def_id) = base_def.opt_def_id() { + if namespace == TypeNS && self.primitive_type_items.contains(&def_id) { + let name = &path.segments[path.segments.len() - path_depth - 1] + .identifier.unhygienic_name; + if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(name) { + return Some(PathResolution::new(Def::PrimTy(*prim_ty), path_depth)) + } + } + } + } + + resolution + } + + /// Skips `path_depth` trailing segments, which is also reflected in the + /// returned value. See `middle::def::PathResolution` for more info. + pub fn resolve_path_noprim(&mut self, + id: NodeId, + path: &Path, + path_depth: usize, + namespace: Namespace) + -> Option { let span = path.span; let segments = &path.segments[..path.segments.len() - path_depth]; @@ -2616,37 +2642,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { // Try to find a path to an item in a module. let last_ident = segments.last().unwrap().identifier; - // Resolve a single identifier with fallback to primitive types - let resolve_identifier_with_fallback = |this: &mut Self, record_used| { - let def = this.resolve_identifier(last_ident, namespace, record_used); - match def { - None | Some(LocalDef{def: Def::Mod(..), ..}) if namespace == TypeNS => - this.primitive_type_table - .primitive_types - .get(&last_ident.unhygienic_name) - .map_or(def, |prim_ty| Some(LocalDef::from_def(Def::PrimTy(*prim_ty)))), - _ => def - } - }; - if segments.len() == 1 { - // In `a(::assoc_item)*` `a` cannot be a module. If `a` does resolve to a module we - // don't report an error right away, but try to fallback to a primitive type. - // So, we are still able to successfully resolve something like - // - // use std::u8; // bring module u8 in scope - // fn f() -> u8 { // OK, resolves to primitive u8, not to std::u8 - // u8::max_value() // OK, resolves to associated function ::max_value, - // // not to non-existent std::u8::max_value - // } - // - // Such behavior is required for backward compatibility. - // The same fallback is used when `a` resolves to nothing. - let unqualified_def = resolve_identifier_with_fallback(self, true); + let unqualified_def = self.resolve_identifier(last_ident, namespace, true); return unqualified_def.and_then(|def| self.adjust_local_def(def, span)).map(mk_res); } - let unqualified_def = resolve_identifier_with_fallback(self, false); + let unqualified_def = self.resolve_identifier(last_ident, namespace, false); let def = self.resolve_module_relative_path(span, segments, namespace); match (def, unqualified_def) { (Some(d), Some(ref ud)) if d == ud.def => { @@ -2932,17 +2933,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { span: Span, name_path: &[ast::Name]) -> Option> { - let last_name = name_path.last().unwrap(); - - if name_path.len() == 1 { - match this.primitive_type_table.primitive_types.get(last_name) { - Some(_) => None, - None => this.current_module.resolve_name(*last_name, TypeNS, true).success() - .and_then(NameBinding::module) - } - } else { - this.resolve_module_path(&name_path, UseLexicalScope, span).success() - } + this.resolve_module_path(&name_path, UseLexicalScope, span).success() } fn is_static_method(this: &Resolver, did: DefId) -> bool { diff --git a/src/librustc_unicode/lib.rs b/src/librustc_unicode/lib.rs index fb85176340e92..731b78ce7ce2c 100644 --- a/src/librustc_unicode/lib.rs +++ b/src/librustc_unicode/lib.rs @@ -34,10 +34,12 @@ #![feature(core_char_ext)] #![feature(lang_items)] +#![cfg_attr(not(stage0), feature(primitive_type))] #![feature(staged_api)] mod tables; mod u_str; +#[cfg_attr(not(stage0), primitive_type)] pub mod char; #[allow(deprecated)] diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 96534f817f80e..7e0d3d9119e81 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -1461,7 +1461,6 @@ mod tests { use io::{ErrorKind, SeekFrom}; use path::Path; use rand::{StdRng, Rng}; - use str; use sys_common::io::test::{TempDir, tmpdir}; #[cfg(windows)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 85e48f85d3d90..bc050e9fadabe 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -247,6 +247,7 @@ #![feature(oom)] #![feature(optin_builtin_traits)] #![feature(placement_in_syntax)] +#![cfg_attr(not(stage0), feature(primitive_type))] #![feature(rand)] #![feature(raw)] #![feature(repr_simd)] @@ -382,6 +383,9 @@ pub mod prelude; // doc pages are inlined from the public re-exports of core_collections::{slice, // str} above. +#[stable(feature = "core_primitive_types", since = "1.9.0")] +pub use core::bool; + #[stable(feature = "rust1", since = "1.0.0")] pub use core::isize; #[stable(feature = "rust1", since = "1.0.0")] @@ -404,7 +408,9 @@ pub use core::u32; #[stable(feature = "rust1", since = "1.0.0")] pub use core::u64; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/f32.rs"] pub mod f32; +#[cfg_attr(not(stage0), primitive_type)] #[path = "num/f64.rs"] pub mod f64; pub mod ascii; diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index 9ca5b445c86a9..df0e821dff3d0 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -51,3 +51,8 @@ #[doc(no_inline)] pub use string::{String, ToString}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use vec::Vec; + + +#[stable(feature = "core_primitive_types", since = "1.9.0")] +#[doc(no_inline)] pub use {u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, + f32, f64, bool, char, str}; diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 5813d82a315a6..c8c6a36a907f7 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -19,7 +19,6 @@ use ffi::OsStr; use fmt; use io; use path::Path; -use str; use sys::pipe::{read2, AnonPipe}; use sys::process as imp; use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; @@ -552,7 +551,6 @@ mod tests { use io::prelude::*; use io::ErrorKind; - use str; use super::{Command, Output, Stdio}; // FIXME(#10380) these tests should not all be ignored on android. diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 64468be396f34..0a429738efc7d 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -394,7 +394,6 @@ mod tests { use sync::atomic::{AtomicUsize, Ordering}; use thread; use time::Duration; - use u32; #[test] fn smoke() { diff --git a/src/libstd/sys/windows/handle.rs b/src/libstd/sys/windows/handle.rs index 1396d670902bb..2e6379bef44a1 100644 --- a/src/libstd/sys/windows/handle.rs +++ b/src/libstd/sys/windows/handle.rs @@ -21,7 +21,6 @@ use ptr; use sys::c; use sys::cvt; use sys_common::io::read_to_end_uninitialized; -use u32; /// An owned container for `HANDLE` object, closing them on Drop. /// diff --git a/src/libstd/sys/windows/stdio.rs b/src/libstd/sys/windows/stdio.rs index 0a0851ffac37a..3624b7af15730 100644 --- a/src/libstd/sys/windows/stdio.rs +++ b/src/libstd/sys/windows/stdio.rs @@ -16,7 +16,6 @@ use io::prelude::*; use cmp; use io::{self, Cursor}; use ptr; -use str; use sync::Mutex; use sys::c; use sys::cvt; diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 2cf6c64eab891..4a49fc09f36df 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -603,7 +603,6 @@ mod tests { use super::{Builder}; use thread; use time::Duration; - use u32; // !!! These tests are dangerous. If something is buggy, they will hang, !!! // !!! instead of exiting cleanly. This might wedge the buildbots. !!! diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index a017e62d54631..f90c8b86202f6 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -89,6 +89,8 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option, Status // rustc internal. ("pushpop_unsafe", "1.2.0", None, Active), + ("local_prelude", "1.9.0", None, Active), + ("primitive_type", "1.9.0", None, Active), ("on_unimplemented", "1.0.0", Some(29628), Active), ("simd_ffi", "1.0.0", Some(27731), Active), @@ -416,6 +418,9 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat "unboxed_closures are still evolving")), ("rustc_reflect_like", Whitelisted, Gated("reflect", "defining reflective traits is still evolving")), + // Items marked with #[primitive_type] resolve to primitive types when used in type context + ("primitive_type", Whitelisted, Gated("primitive_type", + "primitive_type is internal to rustc and libcore")), // Crate level attributes ("crate_name", CrateLevel, Ungated), @@ -426,6 +431,8 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat ("no_main", CrateLevel, Ungated), ("no_builtins", CrateLevel, Ungated), ("recursion_limit", CrateLevel, Ungated), + ("local_prelude", CrateLevel, Gated("local_prelude", + "this attribute is an implementation detail of libcore")), ]; macro_rules! cfg_fn { diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 9049b21d8b4bb..e78c97c1ad8e8 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -52,10 +52,10 @@ pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option) } pub fn maybe_inject_prelude(sess: &ParseSess, krate: ast::Crate) -> ast::Crate { - if no_core(&krate) { + if no_core(&krate) && !local_prelude(&krate) { krate } else { - let name = if no_std(&krate) {"core"} else {"std"}; + let name = if local_prelude(&krate) {""} else if no_std(&krate) {"core"} else {"std"}; let mut fold = PreludeInjector { span: ignored_span(sess, DUMMY_SP), crate_identifier: token::str_to_ident(name), @@ -64,6 +64,10 @@ pub fn maybe_inject_prelude(sess: &ParseSess, krate: ast::Crate) -> ast::Crate { } } +pub fn local_prelude(krate: &ast::Crate) -> bool { + attr::contains_name(&krate.attrs, "local_prelude") +} + pub fn no_core(krate: &ast::Crate) -> bool { attr::contains_name(&krate.attrs, "no_core") } @@ -126,7 +130,7 @@ impl fold::Folder for PreludeInjector { } fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod { - let prelude_path = ast::Path { + let mut prelude_path = ast::Path { span: self.span, global: false, segments: vec![ @@ -144,6 +148,9 @@ impl fold::Folder for PreludeInjector { }, ], }; + if self.crate_identifier.name.as_str().is_empty() { + prelude_path.segments.remove(0); + } let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path))); mod_.items.insert(0, P(ast::Item { diff --git a/src/test/compile-fail/associated-types-issue-20346.rs b/src/test/compile-fail/associated-types-issue-20346.rs index a00aa8364bde2..5968b6f8e670c 100644 --- a/src/test/compile-fail/associated-types-issue-20346.rs +++ b/src/test/compile-fail/associated-types-issue-20346.rs @@ -15,6 +15,7 @@ use std::option::Option::{self, None, Some}; use std::vec::Vec; +use std::bool; trait Iterator { type Item; diff --git a/src/test/compile-fail/import-shadow-1.rs b/src/test/compile-fail/import-shadow-1.rs index 503fa4eca527b..c56b827827635 100644 --- a/src/test/compile-fail/import-shadow-1.rs +++ b/src/test/compile-fail/import-shadow-1.rs @@ -16,11 +16,11 @@ use foo::*; use bar::*; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-2.rs b/src/test/compile-fail/import-shadow-2.rs index 0c107cf27f592..dac85b8fed84e 100644 --- a/src/test/compile-fail/import-shadow-2.rs +++ b/src/test/compile-fail/import-shadow-2.rs @@ -16,11 +16,11 @@ use foo::*; use foo::*; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-3.rs b/src/test/compile-fail/import-shadow-3.rs index bf90973c2857e..b6be4f0e00c6e 100644 --- a/src/test/compile-fail/import-shadow-3.rs +++ b/src/test/compile-fail/import-shadow-3.rs @@ -16,11 +16,11 @@ use foo::Baz; use bar::*; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-4.rs b/src/test/compile-fail/import-shadow-4.rs index f21fdaae47ba0..e2516aaa4db7d 100644 --- a/src/test/compile-fail/import-shadow-4.rs +++ b/src/test/compile-fail/import-shadow-4.rs @@ -16,11 +16,11 @@ use foo::*; use bar::Baz; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-5.rs b/src/test/compile-fail/import-shadow-5.rs index dc300bc7baa77..2569f3339c806 100644 --- a/src/test/compile-fail/import-shadow-5.rs +++ b/src/test/compile-fail/import-shadow-5.rs @@ -16,11 +16,11 @@ use foo::Baz; use bar::Baz; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-6.rs b/src/test/compile-fail/import-shadow-6.rs index fa3b75c70f0b6..a8bded3eb15df 100644 --- a/src/test/compile-fail/import-shadow-6.rs +++ b/src/test/compile-fail/import-shadow-6.rs @@ -16,11 +16,11 @@ use qux::*; use foo::*; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/import-shadow-7.rs b/src/test/compile-fail/import-shadow-7.rs index 34aba15b39228..df678c6f4072a 100644 --- a/src/test/compile-fail/import-shadow-7.rs +++ b/src/test/compile-fail/import-shadow-7.rs @@ -16,11 +16,11 @@ use foo::*; use qux::*; //~ERROR a type named `Baz` has already been imported in this module mod foo { - pub type Baz = isize; + pub type Baz = (); } mod bar { - pub type Baz = isize; + pub type Baz = (); } mod qux { diff --git a/src/test/compile-fail/issue-19660.rs b/src/test/compile-fail/issue-19660.rs index c4b871a28c57d..b0c723194807a 100644 --- a/src/test/compile-fail/issue-19660.rs +++ b/src/test/compile-fail/issue-19660.rs @@ -10,11 +10,13 @@ // error-pattern: requires `copy` lang_item -#![feature(lang_items, start, no_core)] +#![feature(lang_items, start, no_core, primitive_type)] #![no_core] #[lang = "sized"] trait Sized { } +#[primitive_type] type isize = isize; +#[primitive_type] type u8 = u8; struct S; diff --git a/src/test/compile-fail/privacy1.rs b/src/test/compile-fail/privacy1.rs index 9b11eafaa63c3..0754ef89aad8b 100644 --- a/src/test/compile-fail/privacy1.rs +++ b/src/test/compile-fail/privacy1.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(lang_items, start, no_core)] +#![feature(lang_items, start, no_core, primitive_type)] #![no_core] // makes debugging this test *a lot* easier (during resolve) #[lang="sized"] @@ -17,8 +17,12 @@ pub trait Sized {} #[lang="copy"] pub trait Copy {} +#[primitive_type] type isize = isize; +#[primitive_type] type u8 = u8; + mod bar { // shouldn't bring in too much + #[primitive_type] type isize = isize; pub use self::glob::*; // can't publicly re-export private items @@ -93,6 +97,9 @@ fn lol() { } mod foo { + #[primitive_type] type isize = isize; + #[primitive_type] type f32 = f32; + fn test() { ::bar::A::foo(); ::bar::A::bar(); //~ ERROR: method `bar` is private diff --git a/src/test/compile-fail/privacy2.rs b/src/test/compile-fail/privacy2.rs index abf702204d16b..ab455dddb5ad6 100644 --- a/src/test/compile-fail/privacy2.rs +++ b/src/test/compile-fail/privacy2.rs @@ -8,11 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(start, no_core)] +#![feature(start, no_core, primitive_type)] #![no_core] // makes debugging this test *a lot* easier (during resolve) // Test to make sure that globs don't leak in regular `use` statements. +#[primitive_type] type isize = isize; +#[primitive_type] type u8 = u8; + mod bar { pub use self::glob::*; diff --git a/src/test/compile-fail/privacy3.rs b/src/test/compile-fail/privacy3.rs index 89f38fa143445..4f293e06225cb 100644 --- a/src/test/compile-fail/privacy3.rs +++ b/src/test/compile-fail/privacy3.rs @@ -8,12 +8,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(start, no_core)] +#![feature(start, no_core, primitive_type)] #![no_core] // makes debugging this test *a lot* easier (during resolve) // Test to make sure that private items imported through globs remain private // when they're used. +#[primitive_type] type isize = isize; +#[primitive_type] type u8 = u8; + mod bar { pub use self::glob::*; diff --git a/src/test/compile-fail/privacy4.rs b/src/test/compile-fail/privacy4.rs index d9f767442845c..93d9c4c335d8e 100644 --- a/src/test/compile-fail/privacy4.rs +++ b/src/test/compile-fail/privacy4.rs @@ -8,11 +8,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(lang_items, start, no_core)] +#![feature(lang_items, start, no_core, primitive_type)] #![no_core] // makes debugging this test *a lot* easier (during resolve) #[lang = "sized"] pub trait Sized {} #[lang="copy"] pub trait Copy {} +#[primitive_type] type isize = isize; +#[primitive_type] type u8 = u8; // Test to make sure that private items imported through globs remain private // when they're used. diff --git a/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs b/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs index 660c1fa9a88d8..06a433f2d151f 100644 --- a/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs +++ b/src/test/compile-fail/tag-that-dare-not-speak-its-name.rs @@ -12,6 +12,7 @@ #![no_implicit_prelude] use std::vec::Vec; +use std::{char, str, u32}; fn last(v: Vec<&T> ) -> std::option::Option { panic!(); diff --git a/src/test/run-make/simd-ffi/simd.rs b/src/test/run-make/simd-ffi/simd.rs index 49fec6f3619e4..6b4f3114c4806 100644 --- a/src/test/run-make/simd-ffi/simd.rs +++ b/src/test/run-make/simd-ffi/simd.rs @@ -12,11 +12,13 @@ #![crate_type = "lib"] // we can compile to a variety of platforms, because we don't need // cross-compiled standard libraries. -#![feature(no_core)] +#![feature(no_core, primitive_type)] #![no_core] #![feature(repr_simd, simd_ffi, link_llvm_intrinsics, lang_items)] +#[primitive_type] type i32 = i32; +#[primitive_type] type f32 = f32; #[repr(C)] #[derive(Copy)] diff --git a/src/test/run-make/target-specs/foo.rs b/src/test/run-make/target-specs/foo.rs index 15b5697723216..88044ffe827d2 100644 --- a/src/test/run-make/target-specs/foo.rs +++ b/src/test/run-make/target-specs/foo.rs @@ -8,9 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(lang_items, no_core)] +#![feature(lang_items, no_core, primitive_type)] #![no_core] +#[primitive_type] type u8 = u8; +#[primitive_type] type isize = isize; + #[lang="copy"] trait Copy { } diff --git a/src/test/run-pass/num-wrapping.rs b/src/test/run-pass/num-wrapping.rs index 143759e271561..1226568286a0e 100644 --- a/src/test/run-pass/num-wrapping.rs +++ b/src/test/run-pass/num-wrapping.rs @@ -12,7 +12,7 @@ // // Test std::num::Wrapping for {uN, iN, usize, isize} -#![feature(test)] +#![feature(test, primitive_type)] extern crate test; @@ -26,6 +26,7 @@ use test::black_box; macro_rules! int_modules { ($(($name:ident, $size:expr),)*) => ($( + #[primitive_type] mod $name { pub const BITS: usize = $size; pub use std::$name::*; diff --git a/src/test/run-pass/smallest-hello-world.rs b/src/test/run-pass/smallest-hello-world.rs index b11970560d59a..6ad399cf7f830 100644 --- a/src/test/run-pass/smallest-hello-world.rs +++ b/src/test/run-pass/smallest-hello-world.rs @@ -12,7 +12,7 @@ // pretty-expanded FIXME #23616 -#![feature(intrinsics, lang_items, start, no_core, libc)] +#![feature(intrinsics, lang_items, start, no_core, libc, primitive_type)] #![no_core] extern crate libc; @@ -25,6 +25,9 @@ extern "rust-intrinsic" { fn transmute(t: T) -> U; } #[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} } #[no_mangle] pub extern fn rust_eh_register_frames () {} #[no_mangle] pub extern fn rust_eh_unregister_frames () {} +#[primitive_type] type isize = isize; +#[primitive_type] type usize = usize; +#[primitive_type] type u8 = u8; #[start] fn main(_: isize, _: *const *const u8) -> isize { diff --git a/src/test/run-pass/use.rs b/src/test/run-pass/use.rs index 09a3849b9153b..b660670ffeb18 100644 --- a/src/test/run-pass/use.rs +++ b/src/test/run-pass/use.rs @@ -27,4 +27,4 @@ mod baz { } #[start] -pub fn start(_: isize, _: *const *const u8) -> isize { 0 } +pub fn start(_: std::isize, _: *const *const ::std::u8) -> std::isize { 0 }