Skip to content

Commit

Permalink
Lift the restriction on reusing names of primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 24, 2016
1 parent 8d4b1d1 commit 77f033b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 98 deletions.
45 changes: 0 additions & 45 deletions src/librustc_resolve/diagnostics.rs
Expand Up @@ -205,51 +205,6 @@ about what constitutes an Item declaration and what does not:
https://doc.rust-lang.org/reference.html#statements
"##,

E0317: r##"
User-defined types or type parameters cannot shadow the primitive types.
This error indicates you tried to define a type, struct or enum with the same
name as an existing primitive type:
```compile_fail
struct u8 {
// ...
}
```
To fix this, simply name it something else.
Such an error may also occur if you define a type parameter which shadows a
primitive type. An example would be something like:
```compile_fail
impl<u8> MyTrait for Option<u8> {
// ...
}
```
In such a case, if you meant for `u8` to be a generic type parameter (i.e. any
type can be used in its place), use something like `T` instead:
```ignore
impl<T> MyTrait for Option<T> {
// ...
}
```
On the other hand, if you wished to refer to the specific type `u8`, remove it
from the type parameter list:
```ignore
impl MyTrait for Option<u8> {
// ...
}
See the Types section of the reference for more information about the primitive
types:
https://doc.rust-lang.org/reference.html#types
"##,

E0364: r##"
Private items cannot be publicly re-exported. This error indicates that you
attempted to `pub use` a type or value that was not itself public.
Expand Down
40 changes: 1 addition & 39 deletions src/librustc_resolve/lib.rs
Expand Up @@ -1615,15 +1615,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
intravisit::walk_crate(self, krate);
}

fn check_if_primitive_type_name(&self, name: Name, span: Span) {
if let Some(_) = self.primitive_type_table.primitive_types.get(&name) {
span_err!(self.session,
span,
E0317,
"user-defined types or type parameters cannot shadow the primitive types");
}
}

fn resolve_item(&mut self, item: &Item) {
let name = item.name;

Expand All @@ -1633,8 +1624,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ItemEnum(_, ref generics) |
ItemTy(_, ref generics) |
ItemStruct(_, ref generics) => {
self.check_if_primitive_type_name(name, item.span);

self.with_type_parameter_rib(HasTypeParameters(generics, TypeSpace, ItemRibKind),
|this| intravisit::walk_item(this, item));
}
Expand All @@ -1655,8 +1644,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

ItemTrait(_, ref generics, ref bounds, ref trait_items) => {
self.check_if_primitive_type_name(name, item.span);

// Create a new rib for the trait-wide type parameters.
self.with_type_parameter_rib(HasTypeParameters(generics,
TypeSpace,
Expand Down Expand Up @@ -1691,8 +1678,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
});
}
hir::TypeTraitItem(..) => {
this.check_if_primitive_type_name(trait_item.name,
trait_item.span);
this.with_type_parameter_rib(NoTypeParameters, |this| {
intravisit::walk_trait_item(this, trait_item)
});
Expand All @@ -1716,28 +1701,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

ItemUse(ref view_path) => {
// check for imports shadowing primitive types
let check_rename = |this: &Self, id, name| {
match this.def_map.borrow().get(&id).map(|d| d.full_def()) {
Some(Def::Enum(..)) | Some(Def::TyAlias(..)) | Some(Def::Struct(..)) |
Some(Def::Trait(..)) | None => {
this.check_if_primitive_type_name(name, item.span);
}
_ => {}
}
};

match view_path.node {
hir::ViewPathSimple(name, _) => {
check_rename(self, item.id, name);
}
hir::ViewPathList(ref prefix, ref items) => {
for item in items {
if let Some(name) = item.node.rename() {
check_rename(self, item.node.id(), name);
}
}

// Resolve prefix of an import with empty braces (issue #28388)
if items.is_empty() && !prefix.segments.is_empty() {
match self.resolve_crate_relative_path(prefix.span,
Expand Down Expand Up @@ -1918,9 +1883,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

fn resolve_generics(&mut self, generics: &Generics) {
for type_parameter in generics.ty_params.iter() {
self.check_if_primitive_type_name(type_parameter.name, type_parameter.span);
}
for predicate in &generics.where_clause.predicates {
match predicate {
&hir::WherePredicate::BoundPredicate(_) |
Expand Down Expand Up @@ -2699,7 +2661,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
-> Option<LocalDef> {
let def = self.resolve_identifier(identifier, namespace, check_ribs, record_used);
match def {
None | Some(LocalDef{def: Def::Mod(..), ..}) => {
None | Some(LocalDef{def: Def::Mod(..), ..}) if namespace == TypeNS => {
if let Some(&prim_ty) = self.primitive_type_table
.primitive_types
.get(&identifier.unhygienic_name) {
Expand Down
@@ -1,4 +1,4 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -15,12 +15,9 @@ static i32: i32 = 0;
const i64: i64 = 0;
fn u8(f32: f32) {}
fn f<f64>(f64: f64) {}
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
type u16 = u16; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
//~^ ERROR unsupported cyclic reference between types/traits detected
enum u32 {} //~ ERROR user-defined types or type parameters cannot shadow the primitive types
struct u64; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
trait bool {} //~ ERROR user-defined types or type parameters cannot shadow the primitive types
enum u32 {}
struct u64;
trait bool {}

mod char {
extern crate i8;
Expand All @@ -41,29 +38,40 @@ mod char {
use super::u8_ as u8;
use super::f_ as f64;
use super::u16_ as u16;
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
use super::u32_ as u32;
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
use super::u64_ as u64;
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
use super::bool_ as bool;
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
use super::{bool_ as str};
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
use super::char_ as char;
}
}

trait isize_ {
type isize; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
type isize;
}

fn usize<'usize>(usize: &'usize usize) -> &'usize usize { usize }

mod reuse {
use std::mem::size_of;

type u8 = u64;
use std::string::String as i16;

pub fn check<u16>() {
assert_eq!(size_of::<u8>(), 8);
assert_eq!(size_of::<::u64>(), 0);
assert_eq!(size_of::<i16>(), 3 * size_of::<*const ()>());
assert_eq!(size_of::<u16>(), 0);
}
}

fn main() {
let bool = true;
match bool {
let _ = match bool {
str @ true => if str { i32 as i64 } else { i64 },
false => i64,
};

reuse::check::<u64>();
}

0 comments on commit 77f033b

Please sign in to comment.