Skip to content

Commit 2606f99

Browse files
committed
Remove the unneeded Sized bound on TypeId creation
This bound is probably unintentional and is unnecessarily constricting. To facilitate this change, it was also necessary to modify resolve to recurse on and resolve type parameters in extern { } blocks. This fixes an ICE when using bounds on type parameters during the declaration of intrinsics. This also adds tests for TypeId on both Sized and Unsized tests as well as a test for using type parameters and bounds in extern { } blocks.
1 parent dfd557b commit 2606f99

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/libcore/intrinsics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#![experimental]
4343
#![allow(missing_docs)]
4444

45+
use marker::Sized;
46+
4547
pub type GlueFn = extern "Rust" fn(*const i8);
4648

4749
#[lang="ty_desc"]
@@ -200,6 +202,10 @@ extern "rust-intrinsic" {
200202
/// Gets an identifier which is globally unique to the specified type. This
201203
/// function will return the same value for a type regardless of whichever
202204
/// crate it is invoked in.
205+
#[cfg(not(stage0))]
206+
pub fn type_id<T: ?Sized + 'static>() -> TypeId;
207+
208+
#[cfg(stage0)]
203209
pub fn type_id<T: 'static>() -> TypeId;
204210

205211
/// Create a value initialized to zero.
@@ -551,8 +557,15 @@ pub struct TypeId {
551557

552558
impl TypeId {
553559
/// Returns the `TypeId` of the type this generic function has been instantiated with
560+
#[cfg(not(stage0))]
561+
pub fn of<T: ?Sized + 'static>() -> TypeId {
562+
unsafe { type_id::<T>() }
563+
}
564+
565+
#[cfg(stage0)]
554566
pub fn of<T: 'static>() -> TypeId {
555567
unsafe { type_id::<T>() }
556568
}
569+
557570
pub fn hash(&self) -> u64 { self.t }
558571
}

src/libcoretest/intrinsics.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::intrinsics::TypeId;
12+
13+
#[test]
14+
fn test_typeid_sized_types() {
15+
struct X; struct Y(uint);
16+
17+
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
18+
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
19+
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
20+
}
21+
22+
#[test]
23+
fn test_typeid_unsized_types() {
24+
trait Z {}
25+
struct X(str); struct Y(Z + 'static);
26+
27+
assert_eq!(TypeId::of::<X>(), TypeId::of::<X>());
28+
assert_eq!(TypeId::of::<Y>(), TypeId::of::<Y>());
29+
assert!(TypeId::of::<X>() != TypeId::of::<Y>());
30+
}
31+

src/librustc_resolve/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,8 +2943,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29432943
HasTypeParameters(
29442944
generics, FnSpace, foreign_item.id,
29452945
ItemRibKind),
2946-
|this| visit::walk_foreign_item(this,
2947-
&**foreign_item));
2946+
|this| {
2947+
this.resolve_type_parameters(&generics.ty_params);
2948+
this.resolve_where_clause(&generics.where_clause);
2949+
visit::walk_foreign_item(this, &**foreign_item)
2950+
});
29482951
}
29492952
ForeignItemStatic(..) => {
29502953
visit::walk_foreign_item(this,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(intrinsics)]
12+
13+
use std::intrinsics::TypeId;
14+
15+
extern "rust-intrinsic" {
16+
// Real example from libcore
17+
fn type_id<T: ?Sized + 'static>() -> TypeId;
18+
19+
// Silent bounds made explicit to make sure they are actually
20+
// resolved.
21+
fn transmute<T: Sized, U: Sized>(val: T) -> U;
22+
23+
// Bounds aren't checked right now, so this should work
24+
// even though it's incorrect.
25+
fn size_of<T: Clone>() -> uint;
26+
27+
// Unresolved bounds should still error.
28+
fn align_of<T: NoSuchTrait>() -> uint;
29+
//~^ ERROR attempt to bound type parameter with a nonexistent trait `NoSuchTrait`
30+
}
31+
32+
fn main() {}
33+

0 commit comments

Comments
 (0)