From d42da7b8f36fe087b5fe2cb08c136ffd62f73e3d Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Sat, 6 Aug 2016 00:50:13 +0300 Subject: Rewrite TypeId computation to not miss anything and work cross-crate. --- .../run-pass/auxiliary/typeid-intrinsic-aux1.rs | 20 ++++---- .../run-pass/auxiliary/typeid-intrinsic-aux2.rs | 20 ++++---- src/test/run-pass/type-id-higher-rank-2.rs | 40 ++++++++++++++++ src/test/run-pass/type-id-higher-rank.rs | 11 +++++ src/test/run-pass/typeid-intrinsic.rs | 55 +++++++++++----------- 5 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 src/test/run-pass/type-id-higher-rank-2.rs (limited to 'src/test') diff --git a/src/test/run-pass/auxiliary/typeid-intrinsic-aux1.rs b/src/test/run-pass/auxiliary/typeid-intrinsic-aux1.rs index 388d3238d42..42c0da6286b 100644 --- a/src/test/run-pass/auxiliary/typeid-intrinsic-aux1.rs +++ b/src/test/run-pass/auxiliary/typeid-intrinsic-aux1.rs @@ -21,14 +21,16 @@ pub struct E(Result<&'static str, isize>); pub type F = Option; pub type G = usize; pub type H = &'static str; +pub type I = Box; -pub unsafe fn id_A() -> TypeId { TypeId::of::() } -pub unsafe fn id_B() -> TypeId { TypeId::of::() } -pub unsafe fn id_C() -> TypeId { TypeId::of::() } -pub unsafe fn id_D() -> TypeId { TypeId::of::() } -pub unsafe fn id_E() -> TypeId { TypeId::of::() } -pub unsafe fn id_F() -> TypeId { TypeId::of::() } -pub unsafe fn id_G() -> TypeId { TypeId::of::() } -pub unsafe fn id_H() -> TypeId { TypeId::of::() } +pub fn id_A() -> TypeId { TypeId::of::() } +pub fn id_B() -> TypeId { TypeId::of::() } +pub fn id_C() -> TypeId { TypeId::of::() } +pub fn id_D() -> TypeId { TypeId::of::() } +pub fn id_E() -> TypeId { TypeId::of::() } +pub fn id_F() -> TypeId { TypeId::of::() } +pub fn id_G() -> TypeId { TypeId::of::() } +pub fn id_H() -> TypeId { TypeId::of::() } +pub fn id_I() -> TypeId { TypeId::of::() } -pub unsafe fn foo() -> TypeId { TypeId::of::() } +pub fn foo() -> TypeId { TypeId::of::() } diff --git a/src/test/run-pass/auxiliary/typeid-intrinsic-aux2.rs b/src/test/run-pass/auxiliary/typeid-intrinsic-aux2.rs index 3ad307fd3b5..42c0da6286b 100644 --- a/src/test/run-pass/auxiliary/typeid-intrinsic-aux2.rs +++ b/src/test/run-pass/auxiliary/typeid-intrinsic-aux2.rs @@ -21,14 +21,16 @@ pub struct E(Result<&'static str, isize>); pub type F = Option; pub type G = usize; pub type H = &'static str; +pub type I = Box; -pub unsafe fn id_A() -> TypeId { TypeId::of::() } -pub unsafe fn id_B() -> TypeId { TypeId::of::() } -pub unsafe fn id_C() -> TypeId { TypeId::of::() } -pub unsafe fn id_D() -> TypeId { TypeId::of::() } -pub unsafe fn id_E() -> TypeId { TypeId::of::() } -pub unsafe fn id_F() -> TypeId { TypeId::of::() } -pub unsafe fn id_G() -> TypeId { TypeId::of::() } -pub unsafe fn id_H() -> TypeId { TypeId::of::() } +pub fn id_A() -> TypeId { TypeId::of::() } +pub fn id_B() -> TypeId { TypeId::of::() } +pub fn id_C() -> TypeId { TypeId::of::() } +pub fn id_D() -> TypeId { TypeId::of::() } +pub fn id_E() -> TypeId { TypeId::of::() } +pub fn id_F() -> TypeId { TypeId::of::() } +pub fn id_G() -> TypeId { TypeId::of::() } +pub fn id_H() -> TypeId { TypeId::of::() } +pub fn id_I() -> TypeId { TypeId::of::() } -pub unsafe fn foo() -> TypeId { TypeId::of::() } +pub fn foo() -> TypeId { TypeId::of::() } diff --git a/src/test/run-pass/type-id-higher-rank-2.rs b/src/test/run-pass/type-id-higher-rank-2.rs new file mode 100644 index 00000000000..aead8bc264d --- /dev/null +++ b/src/test/run-pass/type-id-higher-rank-2.rs @@ -0,0 +1,40 @@ +// 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. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we can't ignore lifetimes by going through Any. + +use std::any::Any; + +struct Foo<'a>(&'a str); + +fn good(s: &String) -> Foo { Foo(s) } + +fn bad1(s: String) -> Option<&'static str> { + let a: Box = Box::new(good as fn(&String) -> Foo); + a.downcast_ref:: Foo<'static>>().map(|f| f(&s).0) +} + +trait AsStr<'a, 'b> { + fn get(&'a self) -> &'b str; +} + +impl<'a> AsStr<'a, 'a> for String { + fn get(&'a self) -> &'a str { self } +} + +fn bad2(s: String) -> Option<&'static str> { + let a: Box = Box::new(Box::new(s) as Box AsStr<'a, 'a>>); + a.downcast_ref:: AsStr<'a, 'static>>>().map(|x| x.get()) +} + +fn main() { + assert_eq!(bad1(String::from("foo")), None); + assert_eq!(bad2(String::from("bar")), None); +} diff --git a/src/test/run-pass/type-id-higher-rank.rs b/src/test/run-pass/type-id-higher-rank.rs index c29fb5e86f5..827b05c0801 100644 --- a/src/test/run-pass/type-id-higher-rank.rs +++ b/src/test/run-pass/type-id-higher-rank.rs @@ -16,6 +16,9 @@ use std::any::{Any, TypeId}; +struct Struct<'a>(&'a ()); +trait Trait<'a> {} + fn main() { // Bare fns { @@ -34,6 +37,14 @@ fn main() { let e = TypeId::of:: fn(fn(&'a isize) -> &'a isize)>(); let f = TypeId::of:: fn(&'a isize) -> &'a isize)>(); assert!(e != f); + + // Make sure lifetime parameters of items are not ignored. + let g = TypeId::of:: fn(&'a Trait<'a>) -> Struct<'a>>(); + let h = TypeId::of:: fn(&'a Trait<'a>) -> Struct<'static>>(); + let i = TypeId::of:: fn(&'a Trait<'b>) -> Struct<'b>>(); + assert!(g != h); + assert!(g != i); + assert!(h != i); } // Boxed unboxed closures { diff --git a/src/test/run-pass/typeid-intrinsic.rs b/src/test/run-pass/typeid-intrinsic.rs index 4bd82baafeb..e99a5f69af4 100644 --- a/src/test/run-pass/typeid-intrinsic.rs +++ b/src/test/run-pass/typeid-intrinsic.rs @@ -23,36 +23,37 @@ struct A; struct Test; pub fn main() { - unsafe { - assert_eq!(TypeId::of::(), other1::id_A()); - assert_eq!(TypeId::of::(), other1::id_B()); - assert_eq!(TypeId::of::(), other1::id_C()); - assert_eq!(TypeId::of::(), other1::id_D()); - assert_eq!(TypeId::of::(), other1::id_E()); - assert_eq!(TypeId::of::(), other1::id_F()); - assert_eq!(TypeId::of::(), other1::id_G()); - assert_eq!(TypeId::of::(), other1::id_H()); + assert_eq!(TypeId::of::(), other1::id_A()); + assert_eq!(TypeId::of::(), other1::id_B()); + assert_eq!(TypeId::of::(), other1::id_C()); + assert_eq!(TypeId::of::(), other1::id_D()); + assert_eq!(TypeId::of::(), other1::id_E()); + assert_eq!(TypeId::of::(), other1::id_F()); + assert_eq!(TypeId::of::(), other1::id_G()); + assert_eq!(TypeId::of::(), other1::id_H()); + assert_eq!(TypeId::of::(), other1::id_I()); - assert_eq!(TypeId::of::(), other2::id_A()); - assert_eq!(TypeId::of::(), other2::id_B()); - assert_eq!(TypeId::of::(), other2::id_C()); - assert_eq!(TypeId::of::(), other2::id_D()); - assert_eq!(TypeId::of::(), other2::id_E()); - assert_eq!(TypeId::of::(), other2::id_F()); - assert_eq!(TypeId::of::(), other2::id_G()); - assert_eq!(TypeId::of::(), other2::id_H()); + assert_eq!(TypeId::of::(), other2::id_A()); + assert_eq!(TypeId::of::(), other2::id_B()); + assert_eq!(TypeId::of::(), other2::id_C()); + assert_eq!(TypeId::of::(), other2::id_D()); + assert_eq!(TypeId::of::(), other2::id_E()); + assert_eq!(TypeId::of::(), other2::id_F()); + assert_eq!(TypeId::of::(), other2::id_G()); + assert_eq!(TypeId::of::(), other2::id_H()); + assert_eq!(TypeId::of::(), other2::id_I()); - assert_eq!(other1::id_F(), other2::id_F()); - assert_eq!(other1::id_G(), other2::id_G()); - assert_eq!(other1::id_H(), other2::id_H()); + assert_eq!(other1::id_F(), other2::id_F()); + assert_eq!(other1::id_G(), other2::id_G()); + assert_eq!(other1::id_H(), other2::id_H()); + assert_eq!(other1::id_I(), other2::id_I()); - assert_eq!(TypeId::of::(), other2::foo::()); - assert_eq!(TypeId::of::(), other1::foo::()); - assert_eq!(other2::foo::(), other1::foo::()); - assert_eq!(TypeId::of::(), other2::foo::()); - assert_eq!(TypeId::of::(), other1::foo::()); - assert_eq!(other2::foo::(), other1::foo::()); - } + assert_eq!(TypeId::of::(), other2::foo::()); + assert_eq!(TypeId::of::(), other1::foo::()); + assert_eq!(other2::foo::(), other1::foo::()); + assert_eq!(TypeId::of::(), other2::foo::()); + assert_eq!(TypeId::of::(), other1::foo::()); + assert_eq!(other2::foo::(), other1::foo::()); // sanity test of TypeId let (a, b, c) = (TypeId::of::(), TypeId::of::<&'static str>(), -- cgit 1.4.1-3-g733a5