Skip to content

Commit 77f033b

Browse files
committed
Lift the restriction on reusing names of primitive types
1 parent 8d4b1d1 commit 77f033b

File tree

3 files changed

+23
-98
lines changed

3 files changed

+23
-98
lines changed

src/librustc_resolve/diagnostics.rs

-45
Original file line numberDiff line numberDiff line change
@@ -205,51 +205,6 @@ about what constitutes an Item declaration and what does not:
205205
https://doc.rust-lang.org/reference.html#statements
206206
"##,
207207

208-
E0317: r##"
209-
User-defined types or type parameters cannot shadow the primitive types.
210-
This error indicates you tried to define a type, struct or enum with the same
211-
name as an existing primitive type:
212-
213-
```compile_fail
214-
struct u8 {
215-
// ...
216-
}
217-
```
218-
219-
To fix this, simply name it something else.
220-
221-
Such an error may also occur if you define a type parameter which shadows a
222-
primitive type. An example would be something like:
223-
224-
```compile_fail
225-
impl<u8> MyTrait for Option<u8> {
226-
// ...
227-
}
228-
```
229-
230-
In such a case, if you meant for `u8` to be a generic type parameter (i.e. any
231-
type can be used in its place), use something like `T` instead:
232-
233-
```ignore
234-
impl<T> MyTrait for Option<T> {
235-
// ...
236-
}
237-
```
238-
239-
On the other hand, if you wished to refer to the specific type `u8`, remove it
240-
from the type parameter list:
241-
242-
```ignore
243-
impl MyTrait for Option<u8> {
244-
// ...
245-
}
246-
247-
See the Types section of the reference for more information about the primitive
248-
types:
249-
250-
https://doc.rust-lang.org/reference.html#types
251-
"##,
252-
253208
E0364: r##"
254209
Private items cannot be publicly re-exported. This error indicates that you
255210
attempted to `pub use` a type or value that was not itself public.

src/librustc_resolve/lib.rs

+1-39
Original file line numberDiff line numberDiff line change
@@ -1615,15 +1615,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16151615
intravisit::walk_crate(self, krate);
16161616
}
16171617

1618-
fn check_if_primitive_type_name(&self, name: Name, span: Span) {
1619-
if let Some(_) = self.primitive_type_table.primitive_types.get(&name) {
1620-
span_err!(self.session,
1621-
span,
1622-
E0317,
1623-
"user-defined types or type parameters cannot shadow the primitive types");
1624-
}
1625-
}
1626-
16271618
fn resolve_item(&mut self, item: &Item) {
16281619
let name = item.name;
16291620

@@ -1633,8 +1624,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16331624
ItemEnum(_, ref generics) |
16341625
ItemTy(_, ref generics) |
16351626
ItemStruct(_, ref generics) => {
1636-
self.check_if_primitive_type_name(name, item.span);
1637-
16381627
self.with_type_parameter_rib(HasTypeParameters(generics, TypeSpace, ItemRibKind),
16391628
|this| intravisit::walk_item(this, item));
16401629
}
@@ -1655,8 +1644,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16551644
}
16561645

16571646
ItemTrait(_, ref generics, ref bounds, ref trait_items) => {
1658-
self.check_if_primitive_type_name(name, item.span);
1659-
16601647
// Create a new rib for the trait-wide type parameters.
16611648
self.with_type_parameter_rib(HasTypeParameters(generics,
16621649
TypeSpace,
@@ -1691,8 +1678,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16911678
});
16921679
}
16931680
hir::TypeTraitItem(..) => {
1694-
this.check_if_primitive_type_name(trait_item.name,
1695-
trait_item.span);
16961681
this.with_type_parameter_rib(NoTypeParameters, |this| {
16971682
intravisit::walk_trait_item(this, trait_item)
16981683
});
@@ -1716,28 +1701,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17161701
}
17171702

17181703
ItemUse(ref view_path) => {
1719-
// check for imports shadowing primitive types
1720-
let check_rename = |this: &Self, id, name| {
1721-
match this.def_map.borrow().get(&id).map(|d| d.full_def()) {
1722-
Some(Def::Enum(..)) | Some(Def::TyAlias(..)) | Some(Def::Struct(..)) |
1723-
Some(Def::Trait(..)) | None => {
1724-
this.check_if_primitive_type_name(name, item.span);
1725-
}
1726-
_ => {}
1727-
}
1728-
};
1729-
17301704
match view_path.node {
1731-
hir::ViewPathSimple(name, _) => {
1732-
check_rename(self, item.id, name);
1733-
}
17341705
hir::ViewPathList(ref prefix, ref items) => {
1735-
for item in items {
1736-
if let Some(name) = item.node.rename() {
1737-
check_rename(self, item.node.id(), name);
1738-
}
1739-
}
1740-
17411706
// Resolve prefix of an import with empty braces (issue #28388)
17421707
if items.is_empty() && !prefix.segments.is_empty() {
17431708
match self.resolve_crate_relative_path(prefix.span,
@@ -1918,9 +1883,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19181883
}
19191884

19201885
fn resolve_generics(&mut self, generics: &Generics) {
1921-
for type_parameter in generics.ty_params.iter() {
1922-
self.check_if_primitive_type_name(type_parameter.name, type_parameter.span);
1923-
}
19241886
for predicate in &generics.where_clause.predicates {
19251887
match predicate {
19261888
&hir::WherePredicate::BoundPredicate(_) |
@@ -2699,7 +2661,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26992661
-> Option<LocalDef> {
27002662
let def = self.resolve_identifier(identifier, namespace, check_ribs, record_used);
27012663
match def {
2702-
None | Some(LocalDef{def: Def::Mod(..), ..}) => {
2664+
None | Some(LocalDef{def: Def::Mod(..), ..}) if namespace == TypeNS => {
27032665
if let Some(&prim_ty) = self.primitive_type_table
27042666
.primitive_types
27052667
.get(&identifier.unhygienic_name) {

src/test/compile-fail/issue-20427.rs renamed to src/test/run-pass/issue-20427.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -15,12 +15,9 @@ static i32: i32 = 0;
1515
const i64: i64 = 0;
1616
fn u8(f32: f32) {}
1717
fn f<f64>(f64: f64) {}
18-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
19-
type u16 = u16; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
20-
//~^ ERROR unsupported cyclic reference between types/traits detected
21-
enum u32 {} //~ ERROR user-defined types or type parameters cannot shadow the primitive types
22-
struct u64; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
23-
trait bool {} //~ ERROR user-defined types or type parameters cannot shadow the primitive types
18+
enum u32 {}
19+
struct u64;
20+
trait bool {}
2421

2522
mod char {
2623
extern crate i8;
@@ -41,29 +38,40 @@ mod char {
4138
use super::u8_ as u8;
4239
use super::f_ as f64;
4340
use super::u16_ as u16;
44-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
4541
use super::u32_ as u32;
46-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
4742
use super::u64_ as u64;
48-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
4943
use super::bool_ as bool;
50-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
5144
use super::{bool_ as str};
52-
//~^ ERROR user-defined types or type parameters cannot shadow the primitive types
5345
use super::char_ as char;
5446
}
5547
}
5648

5749
trait isize_ {
58-
type isize; //~ ERROR user-defined types or type parameters cannot shadow the primitive types
50+
type isize;
5951
}
6052

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

55+
mod reuse {
56+
use std::mem::size_of;
57+
58+
type u8 = u64;
59+
use std::string::String as i16;
60+
61+
pub fn check<u16>() {
62+
assert_eq!(size_of::<u8>(), 8);
63+
assert_eq!(size_of::<::u64>(), 0);
64+
assert_eq!(size_of::<i16>(), 3 * size_of::<*const ()>());
65+
assert_eq!(size_of::<u16>(), 0);
66+
}
67+
}
68+
6369
fn main() {
6470
let bool = true;
65-
match bool {
71+
let _ = match bool {
6672
str @ true => if str { i32 as i64 } else { i64 },
6773
false => i64,
6874
};
75+
76+
reuse::check::<u64>();
6977
}

0 commit comments

Comments
 (0)