From 63f51ee90ccc1b7ab3a749ba7dbd4a3c76961c16 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 18 Feb 2015 19:35:20 -0500 Subject: [PATCH] Exempt phantom fns from the object safety check --- src/librustc/middle/traits/object_safety.rs | 9 +++--- .../compile-fail/object-safety-phantom-fn.rs | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/test/compile-fail/object-safety-phantom-fn.rs diff --git a/src/librustc/middle/traits/object_safety.rs b/src/librustc/middle/traits/object_safety.rs index a8f8d54cba9..f10f7eb3951 100644 --- a/src/librustc/middle/traits/object_safety.rs +++ b/src/librustc/middle/traits/object_safety.rs @@ -138,10 +138,11 @@ fn supertraits_reference_self<'tcx>(tcx: &ty::ctxt<'tcx>, match predicate { ty::Predicate::Trait(ref data) => { // In the case of a trait predicate, we can skip the "self" type. - data.0.trait_ref.substs.types.get_slice(TypeSpace) - .iter() - .cloned() - .any(is_self) + Some(data.def_id()) != tcx.lang_items.phantom_fn() && + data.0.trait_ref.substs.types.get_slice(TypeSpace) + .iter() + .cloned() + .any(is_self) } ty::Predicate::Projection(..) | ty::Predicate::TypeOutlives(..) | diff --git a/src/test/compile-fail/object-safety-phantom-fn.rs b/src/test/compile-fail/object-safety-phantom-fn.rs new file mode 100644 index 00000000000..fb30ce5c245 --- /dev/null +++ b/src/test/compile-fail/object-safety-phantom-fn.rs @@ -0,0 +1,28 @@ +// Copyright 2014 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. + +// Check that `Self` appearing in a phantom fn does not make a trait not object safe. + +#![feature(rustc_attrs)] + +trait Baz : PhantomFn { +} + +fn make_bar>(t: &T) -> &Bar { + t +} + +fn make_baz(t: &T) -> &Baz { + t +} + +#[rustc_error] +fn main() { //~ ERROR compilation successful +}