Skip to content

Commit aa4570a

Browse files
committed
Auto merge of #42417 - eddyb:separate-fn-sig, r=<try>
Don't drag function signatures along function item types. **TODO**: write description (made PR for crater/cargobomb shenanigans) r? @nikomatsakis
2 parents e1293ec + 2a64a2d commit aa4570a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+769
-875
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::TypeVariants<'
514514
region.hash_stable(hcx, hasher);
515515
pointee_ty.hash_stable(hcx, hasher);
516516
}
517-
TyFnDef(def_id, substs, ref sig) => {
517+
TyFnDef(def_id, substs) => {
518518
def_id.hash_stable(hcx, hasher);
519519
substs.hash_stable(hcx, hasher);
520-
sig.hash_stable(hcx, hasher);
521520
}
522521
TyFnPtr(ref sig) => {
523522
sig.hash_stable(hcx, hasher);

src/librustc/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
15461546
Some(self.tcx.closure_kind(def_id))
15471547
}
15481548

1549-
pub fn closure_type(&self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
1549+
pub fn fn_sig(&self, def_id: DefId) -> ty::PolyFnSig<'tcx> {
15501550
if let InferTables::InProgress(tables) = self.tables {
15511551
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
15521552
if let Some(&ty) = tables.borrow().closure_tys.get(&id) {
@@ -1555,7 +1555,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
15551555
}
15561556
}
15571557

1558-
self.tcx.closure_type(def_id)
1558+
self.tcx.fn_sig(def_id)
15591559
}
15601560
}
15611561

src/librustc/middle/effect.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! `unsafe`.
1313
use self::RootUnsafeContext::*;
1414

15-
use ty::{self, Ty, TyCtxt};
15+
use ty::{self, TyCtxt};
1616
use lint;
1717

1818
use syntax::ast;
@@ -40,14 +40,6 @@ enum RootUnsafeContext {
4040
UnsafeBlock(ast::NodeId),
4141
}
4242

43-
fn type_is_unsafe_function(ty: Ty) -> bool {
44-
match ty.sty {
45-
ty::TyFnDef(.., f) |
46-
ty::TyFnPtr(f) => f.unsafety() == hir::Unsafety::Unsafe,
47-
_ => false,
48-
}
49-
}
50-
5143
struct EffectCheckVisitor<'a, 'tcx: 'a> {
5244
tcx: TyCtxt<'a, 'tcx, 'tcx>,
5345
tables: &'a ty::TypeckTables<'tcx>,
@@ -174,10 +166,11 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
174166
match expr.node {
175167
hir::ExprMethodCall(..) => {
176168
let def_id = self.tables.type_dependent_defs[&expr.id].def_id();
177-
let base_type = self.tcx.type_of(def_id);
178-
debug!("effect: method call case, base type is {:?}",
179-
base_type);
180-
if type_is_unsafe_function(base_type) {
169+
let sig = self.tcx.fn_sig(def_id);
170+
debug!("effect: method call case, signature is {:?}",
171+
sig);
172+
173+
if sig.0.unsafety == hir::Unsafety::Unsafe {
181174
self.require_unsafe(expr.span,
182175
"invocation of unsafe method")
183176
}
@@ -186,8 +179,13 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
186179
let base_type = self.tables.expr_ty_adjusted(base);
187180
debug!("effect: call case, base type is {:?}",
188181
base_type);
189-
if type_is_unsafe_function(base_type) {
190-
self.require_unsafe(expr.span, "call to unsafe function")
182+
match base_type.sty {
183+
ty::TyFnDef(..) | ty::TyFnPtr(_) => {
184+
if base_type.fn_sig(self.tcx).unsafety() == hir::Unsafety::Unsafe {
185+
self.require_unsafe(expr.span, "call to unsafe function")
186+
}
187+
}
188+
_ => {}
191189
}
192190
}
193191
hir::ExprUnary(hir::UnDeref, ref base) => {

src/librustc/middle/intrinsicck.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6666

6767
impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
6868
fn def_id_is_transmute(&self, def_id: DefId) -> bool {
69-
let intrinsic = match self.tcx.type_of(def_id).sty {
70-
ty::TyFnDef(.., bfty) => bfty.abi() == RustIntrinsic,
71-
_ => return false
72-
};
73-
intrinsic && self.tcx.item_name(def_id) == "transmute"
69+
self.tcx.fn_sig(def_id).abi() == RustIntrinsic &&
70+
self.tcx.item_name(def_id) == "transmute"
7471
}
7572

7673
fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
@@ -159,22 +156,14 @@ impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
159156
} else {
160157
Def::Err
161158
};
162-
match def {
163-
Def::Fn(did) if self.def_id_is_transmute(did) => {
159+
if let Def::Fn(did) = def {
160+
if self.def_id_is_transmute(did) {
164161
let typ = self.tables.node_id_to_type(expr.id);
165-
let typ = self.tcx.lift_to_global(&typ).unwrap();
166-
match typ.sty {
167-
ty::TyFnDef(.., sig) if sig.abi() == RustIntrinsic => {
168-
let from = sig.inputs().skip_binder()[0];
169-
let to = *sig.output().skip_binder();
170-
self.check_transmute(expr.span, from, to);
171-
}
172-
_ => {
173-
span_bug!(expr.span, "transmute wasn't a bare fn?!");
174-
}
175-
}
162+
let sig = typ.fn_sig(self.tcx);
163+
let from = sig.inputs().skip_binder()[0];
164+
let to = *sig.output().skip_binder();
165+
self.check_transmute(expr.span, from, to);
176166
}
177-
_ => {}
178167
}
179168

180169
intravisit::walk_expr(self, expr);

src/librustc/traits/object_safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
260260

261261
// The `Self` type is erased, so it should not appear in list of
262262
// arguments or return type apart from the receiver.
263-
let ref sig = self.type_of(method.def_id).fn_sig();
263+
let ref sig = self.fn_sig(method.def_id);
264264
for input_ty in &sig.skip_binder().inputs()[1..] {
265265
if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
266266
return Some(MethodViolationCode::ReferencesSelf);

src/librustc/traits/project.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,19 @@ fn confirm_fn_pointer_candidate<'cx, 'gcx, 'tcx>(
11371137
-> Progress<'tcx>
11381138
{
11391139
let fn_type = selcx.infcx().shallow_resolve(fn_pointer_vtable.fn_ty);
1140-
let sig = fn_type.fn_sig();
1140+
let sig = fn_type.fn_sig(selcx.tcx());
1141+
let Normalized {
1142+
value: sig,
1143+
obligations
1144+
} = normalize_with_depth(selcx,
1145+
obligation.param_env,
1146+
obligation.cause.clone(),
1147+
obligation.recursion_depth+1,
1148+
&sig);
1149+
11411150
confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
11421151
.with_addl_obligations(fn_pointer_vtable.nested)
1152+
.with_addl_obligations(obligations)
11431153
}
11441154

11451155
fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
@@ -1149,7 +1159,7 @@ fn confirm_closure_candidate<'cx, 'gcx, 'tcx>(
11491159
-> Progress<'tcx>
11501160
{
11511161
let closure_typer = selcx.closure_typer();
1152-
let closure_type = closure_typer.closure_type(vtable.closure_def_id)
1162+
let closure_type = closure_typer.fn_sig(vtable.closure_def_id)
11531163
.subst(selcx.tcx(), vtable.substs.substs);
11541164
let Normalized {
11551165
value: closure_type,

src/librustc/traits/select.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,19 +1402,15 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
14021402
}
14031403

14041404
// provide an impl, but only for suitable `fn` pointers
1405-
ty::TyFnDef(.., ty::Binder(ty::FnSig {
1406-
unsafety: hir::Unsafety::Normal,
1407-
abi: Abi::Rust,
1408-
variadic: false,
1409-
..
1410-
})) |
1411-
ty::TyFnPtr(ty::Binder(ty::FnSig {
1412-
unsafety: hir::Unsafety::Normal,
1413-
abi: Abi::Rust,
1414-
variadic: false,
1415-
..
1416-
})) => {
1417-
candidates.vec.push(FnPointerCandidate);
1405+
ty::TyFnDef(..) | ty::TyFnPtr(_) => {
1406+
if let ty::Binder(ty::FnSig {
1407+
unsafety: hir::Unsafety::Normal,
1408+
abi: Abi::Rust,
1409+
variadic: false,
1410+
..
1411+
}) = self_ty.fn_sig(self.tcx()) {
1412+
candidates.vec.push(FnPointerCandidate);
1413+
}
14181414
}
14191415

14201416
_ => { }
@@ -2346,19 +2342,26 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23462342

23472343
// ok to skip binder; it is reintroduced below
23482344
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
2349-
let sig = self_ty.fn_sig();
2345+
let sig = self_ty.fn_sig(self.tcx());
23502346
let trait_ref =
23512347
self.tcx().closure_trait_ref_and_return_type(obligation.predicate.def_id(),
23522348
self_ty,
23532349
sig,
23542350
util::TupleArgumentsFlag::Yes)
23552351
.map_bound(|(trait_ref, _)| trait_ref);
23562352

2353+
let Normalized { value: trait_ref, obligations } =
2354+
project::normalize_with_depth(self,
2355+
obligation.param_env,
2356+
obligation.cause.clone(),
2357+
obligation.recursion_depth + 1,
2358+
&trait_ref);
2359+
23572360
self.confirm_poly_trait_refs(obligation.cause.clone(),
23582361
obligation.param_env,
23592362
obligation.predicate.to_poly_trait_ref(),
23602363
trait_ref)?;
2361-
Ok(VtableFnPointerData { fn_ty: self_ty, nested: vec![] })
2364+
Ok(VtableFnPointerData { fn_ty: self_ty, nested: obligations })
23622365
}
23632366

23642367
fn confirm_closure_candidate(&mut self,
@@ -2797,7 +2800,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
27972800
substs: ty::ClosureSubsts<'tcx>)
27982801
-> ty::PolyTraitRef<'tcx>
27992802
{
2800-
let closure_type = self.infcx.closure_type(closure_def_id)
2803+
let closure_type = self.infcx.fn_sig(closure_def_id)
28012804
.subst(self.tcx(), substs.substs);
28022805
let ty::Binder((trait_ref, _)) =
28032806
self.tcx().closure_trait_ref_and_return_type(obligation.predicate.def_id(),

src/librustc/ty/context.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,9 +1378,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13781378
}
13791379

13801380
pub fn mk_fn_def(self, def_id: DefId,
1381-
substs: &'tcx Substs<'tcx>,
1382-
fty: PolyFnSig<'tcx>) -> Ty<'tcx> {
1383-
self.mk_ty(TyFnDef(def_id, substs, fty))
1381+
substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
1382+
self.mk_ty(TyFnDef(def_id, substs))
13841383
}
13851384

13861385
pub fn mk_fn_ptr(self, fty: PolyFnSig<'tcx>) -> Ty<'tcx> {

src/librustc/ty/fast_reject.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
6868
// view of possibly unifying
6969
simplify_type(tcx, mt.ty, can_simplify_params)
7070
}
71+
ty::TyFnDef(def_id, _) |
7172
ty::TyClosure(def_id, _) => {
7273
Some(ClosureSimplifiedType(def_id))
7374
}
7475
ty::TyNever => Some(NeverSimplifiedType),
7576
ty::TyTuple(ref tys, _) => {
7677
Some(TupleSimplifiedType(tys.len()))
7778
}
78-
ty::TyFnDef(.., ref f) | ty::TyFnPtr(ref f) => {
79+
ty::TyFnPtr(ref f) => {
7980
Some(FunctionSimplifiedType(f.skip_binder().inputs().len()))
8081
}
8182
ty::TyProjection(_) | ty::TyParam(_) => {

src/librustc/ty/flags.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ impl FlagComputation {
155155
self.add_tys(&ts[..]);
156156
}
157157

158-
&ty::TyFnDef(_, substs, f) => {
158+
&ty::TyFnDef(_, substs) => {
159159
self.add_substs(substs);
160-
self.add_fn_sig(f);
161160
}
162161

163162
&ty::TyFnPtr(f) => {

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
347347
.filter_map(|ty| characteristic_def_id_of_type(ty))
348348
.next(),
349349

350-
ty::TyFnDef(def_id, ..) |
350+
ty::TyFnDef(def_id, _) |
351351
ty::TyClosure(def_id, _) => Some(def_id),
352352

353353
ty::TyBool |

src/librustc/ty/maps.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,8 @@ define_maps! { <'tcx>
845845
/// expression defining the closure.
846846
[] closure_kind: ItemSignature(DefId) -> ty::ClosureKind,
847847

848-
/// Records the type of each closure. The def ID is the ID of the
849-
/// expression defining the closure.
850-
[] closure_type: ItemSignature(DefId) -> ty::PolyFnSig<'tcx>,
848+
/// Records the signature of functions and closures.
849+
[] fn_sig: ItemSignature(DefId) -> ty::PolyFnSig<'tcx>,
851850

852851
/// Caches CoerceUnsized kinds for impls on custom types.
853852
[] coerce_unsized_info: ItemSignature(DefId)

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl AssociatedItem {
206206
// late-bound regions, and we don't want method signatures to show up
207207
// `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
208208
// regions just fine, showing `fn(&MyType)`.
209-
format!("{}", tcx.type_of(self.def_id).fn_sig().skip_binder())
209+
format!("{}", tcx.fn_sig(self.def_id).skip_binder())
210210
}
211211
ty::AssociatedKind::Type => format!("type {};", self.name.to_string()),
212212
ty::AssociatedKind::Const => {

src/librustc/ty/relate.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
291291
if a.def_id != b.def_id {
292292
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
293293
} else {
294-
let substs = relation.relate_item_substs(a.def_id, a.substs, b.substs)?;
294+
let substs = relate_substs(relation, None, a.substs, b.substs)?;
295295
Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
296296
}
297297
}
@@ -308,7 +308,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
308308
if a.def_id != b.def_id {
309309
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
310310
} else {
311-
let substs = relation.relate_item_substs(a.def_id, a.substs, b.substs)?;
311+
let substs = relate_substs(relation, None, a.substs, b.substs)?;
312312
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
313313
}
314314
}
@@ -440,13 +440,11 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
440440
}
441441
}
442442

443-
(&ty::TyFnDef(a_def_id, a_substs, a_fty),
444-
&ty::TyFnDef(b_def_id, b_substs, b_fty))
443+
(&ty::TyFnDef(a_def_id, a_substs), &ty::TyFnDef(b_def_id, b_substs))
445444
if a_def_id == b_def_id =>
446445
{
447-
let substs = relate_substs(relation, None, a_substs, b_substs)?;
448-
let fty = relation.relate(&a_fty, &b_fty)?;
449-
Ok(tcx.mk_fn_def(a_def_id, substs, fty))
446+
let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
447+
Ok(tcx.mk_fn_def(a_def_id, substs))
450448
}
451449

452450
(&ty::TyFnPtr(a_fty), &ty::TyFnPtr(b_fty)) =>

src/librustc/ty/structural_impls.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,8 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
531531
ty::TyDynamic(ref trait_ty, ref region) =>
532532
ty::TyDynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
533533
ty::TyTuple(ts, defaulted) => ty::TyTuple(ts.fold_with(folder), defaulted),
534-
ty::TyFnDef(def_id, substs, f) => {
535-
ty::TyFnDef(def_id,
536-
substs.fold_with(folder),
537-
f.fold_with(folder))
534+
ty::TyFnDef(def_id, substs) => {
535+
ty::TyFnDef(def_id, substs.fold_with(folder))
538536
}
539537
ty::TyFnPtr(f) => ty::TyFnPtr(f.fold_with(folder)),
540538
ty::TyRef(ref r, tm) => {
@@ -568,9 +566,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
568566
ty::TyDynamic(ref trait_ty, ref reg) =>
569567
trait_ty.visit_with(visitor) || reg.visit_with(visitor),
570568
ty::TyTuple(ts, _) => ts.visit_with(visitor),
571-
ty::TyFnDef(_, substs, ref f) => {
572-
substs.visit_with(visitor) || f.visit_with(visitor)
573-
}
569+
ty::TyFnDef(_, substs) => substs.visit_with(visitor),
574570
ty::TyFnPtr(ref f) => f.visit_with(visitor),
575571
ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
576572
ty::TyClosure(_did, ref substs) => substs.visit_with(visitor),

src/librustc/ty/sty.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir::def_id::DefId;
1414
use hir::map::DefPathHash;
1515

1616
use middle::region;
17-
use ty::subst::Substs;
17+
use ty::subst::{Substs, Subst};
1818
use ty::{self, AdtDef, TypeFlags, Ty, TyCtxt, TypeFoldable};
1919
use ty::{Slice, TyS};
2020
use ty::subst::Kind;
@@ -138,7 +138,7 @@ pub enum TypeVariants<'tcx> {
138138

139139
/// The anonymous type of a function declaration/definition. Each
140140
/// function has a unique type.
141-
TyFnDef(DefId, &'tcx Substs<'tcx>, PolyFnSig<'tcx>),
141+
TyFnDef(DefId, &'tcx Substs<'tcx>),
142142

143143
/// A pointer to a function. Written as `fn() -> i32`.
144144
TyFnPtr(PolyFnSig<'tcx>),
@@ -1329,9 +1329,12 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
13291329
}
13301330
}
13311331

1332-
pub fn fn_sig(&self) -> PolyFnSig<'tcx> {
1332+
pub fn fn_sig(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> PolyFnSig<'tcx> {
13331333
match self.sty {
1334-
TyFnDef(.., f) | TyFnPtr(f) => f,
1334+
TyFnDef(def_id, substs) => {
1335+
tcx.fn_sig(def_id).subst(tcx, substs)
1336+
}
1337+
TyFnPtr(f) => f,
13351338
_ => bug!("Ty::fn_sig() called on non-fn type: {:?}", self)
13361339
}
13371340
}

0 commit comments

Comments
 (0)