Skip to content

Commit 1eab19d

Browse files
Refactor ty::FnSig to privatize all fields
1 parent 0999124 commit 1eab19d

Some content is hidden

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

42 files changed

+237
-271
lines changed

src/librustc/middle/intrinsicck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for ExprVisitor<'a, 'gcx, 'tcx> {
178178
let typ = self.infcx.tcx.tables().node_id_to_type(expr.id);
179179
match typ.sty {
180180
ty::TyFnDef(.., ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => {
181-
let from = bare_fn_ty.sig.0.inputs[0];
182-
let to = bare_fn_ty.sig.0.output;
181+
let from = bare_fn_ty.sig.skip_binder().inputs()[0];
182+
let to = bare_fn_ty.sig.skip_binder().output();
183183
self.check_transmute(expr.span, from, to, expr.id);
184184
}
185185
_ => {

src/librustc/traits/object_safety.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
241241
// The `Self` type is erased, so it should not appear in list of
242242
// arguments or return type apart from the receiver.
243243
let ref sig = self.item_type(method.def_id).fn_sig();
244-
for &input_ty in &sig.0.inputs[1..] {
244+
for input_ty in &sig.skip_binder().inputs()[1..] {
245245
if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
246246
return Some(MethodViolationCode::ReferencesSelf);
247247
}
248248
}
249-
if self.contains_illegal_self_type_reference(trait_def_id, sig.0.output) {
249+
if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
250250
return Some(MethodViolationCode::ReferencesSelf);
251251
}
252252

src/librustc/traits/select.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,21 +1368,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
13681368
ty::TyFnDef(.., &ty::BareFnTy {
13691369
unsafety: hir::Unsafety::Normal,
13701370
abi: Abi::Rust,
1371-
sig: ty::Binder(ty::FnSig {
1372-
inputs: _,
1373-
output: _,
1374-
variadic: false
1375-
})
1371+
ref sig,
13761372
}) |
13771373
ty::TyFnPtr(&ty::BareFnTy {
13781374
unsafety: hir::Unsafety::Normal,
13791375
abi: Abi::Rust,
1380-
sig: ty::Binder(ty::FnSig {
1381-
inputs: _,
1382-
output: _,
1383-
variadic: false
1384-
})
1385-
}) => {
1376+
ref sig
1377+
}) if !sig.variadic() => {
13861378
candidates.vec.push(FnPointerCandidate);
13871379
}
13881380

src/librustc/traits/util.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
487487
-> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>)>
488488
{
489489
let arguments_tuple = match tuple_arguments {
490-
TupleArgumentsFlag::No => sig.0.inputs[0],
491-
TupleArgumentsFlag::Yes => self.intern_tup(&sig.0.inputs[..]),
490+
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
491+
TupleArgumentsFlag::Yes =>
492+
self.intern_tup(sig.skip_binder().inputs()),
492493
};
493494
let trait_ref = ty::TraitRef {
494495
def_id: fn_trait_def_id,
495496
substs: self.mk_substs_trait(self_ty, &[arguments_tuple]),
496497
};
497-
ty::Binder((trait_ref, sig.0.output))
498+
ty::Binder((trait_ref, sig.skip_binder().output()))
498499
}
499500
}
500501

src/librustc/ty/fast_reject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
8181
Some(TupleSimplifiedType(tys.len()))
8282
}
8383
ty::TyFnDef(.., ref f) | ty::TyFnPtr(ref f) => {
84-
Some(FunctionSimplifiedType(f.sig.0.inputs.len()))
84+
Some(FunctionSimplifiedType(f.sig.skip_binder().inputs().len()))
8585
}
8686
ty::TyProjection(_) | ty::TyParam(_) => {
8787
if can_simplify_params {

src/librustc/ty/flags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ impl FlagComputation {
180180
fn add_fn_sig(&mut self, fn_sig: &ty::PolyFnSig) {
181181
let mut computation = FlagComputation::new();
182182

183-
computation.add_tys(&fn_sig.0.inputs);
184-
computation.add_ty(fn_sig.0.output);
183+
computation.add_tys(fn_sig.skip_binder().inputs());
184+
computation.add_ty(fn_sig.skip_binder().output());
185185

186186
self.add_bound_computation(&computation);
187187
}

src/librustc/ty/relate.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -180,35 +180,22 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
180180
-> RelateResult<'tcx, ty::FnSig<'tcx>>
181181
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
182182
{
183-
if a.variadic != b.variadic {
183+
if a.variadic() != b.variadic() {
184184
return Err(TypeError::VariadicMismatch(
185-
expected_found(relation, &a.variadic, &b.variadic)));
185+
expected_found(relation, &a.variadic(), &b.variadic())));
186186
}
187187

188-
let inputs = relate_arg_vecs(relation,
189-
&a.inputs,
190-
&b.inputs)?;
191-
let output = relation.relate(&a.output, &b.output)?;
188+
if a.inputs().len() != b.inputs().len() {
189+
return Err(TypeError::ArgCount);
190+
}
192191

193-
Ok(ty::FnSig {inputs: inputs,
194-
output: output,
195-
variadic: a.variadic})
196-
}
197-
}
192+
let inputs = a.inputs().iter().zip(b.inputs()).map(|(&a, &b)| {
193+
relation.relate_with_variance(ty::Contravariant, &a, &b)
194+
}).collect::<Result<Vec<_>, _>>()?;
195+
let output = relation.relate(&a.output(), &b.output())?;
198196

199-
fn relate_arg_vecs<'a, 'gcx, 'tcx, R>(relation: &mut R,
200-
a_args: &[Ty<'tcx>],
201-
b_args: &[Ty<'tcx>])
202-
-> RelateResult<'tcx, Vec<Ty<'tcx>>>
203-
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
204-
{
205-
if a_args.len() != b_args.len() {
206-
return Err(TypeError::ArgCount);
197+
Ok(ty::FnSig::new(inputs, output, a.variadic()))
207198
}
208-
209-
a_args.iter().zip(b_args)
210-
.map(|(a, b)| relation.relate_with_variance(ty::Contravariant, a, b))
211-
.collect()
212199
}
213200

214201
impl<'tcx> Relate<'tcx> for ast::Unsafety {

src/librustc/ty/structural_impls.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,9 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
232232
impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
233233
type Lifted = ty::FnSig<'tcx>;
234234
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
235-
tcx.lift(&self.inputs[..]).and_then(|inputs| {
236-
tcx.lift(&self.output).map(|output| {
237-
ty::FnSig {
238-
inputs: inputs,
239-
output: output,
240-
variadic: self.variadic
241-
}
235+
tcx.lift(self.inputs()).and_then(|inputs| {
236+
tcx.lift(&self.output()).map(|output| {
237+
ty::FnSig::new(inputs, output, self.variadic())
242238
})
243239
})
244240
}
@@ -589,17 +585,17 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
589585

590586
impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
591587
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
592-
ty::FnSig { inputs: self.inputs.fold_with(folder),
593-
output: self.output.fold_with(folder),
594-
variadic: self.variadic }
588+
ty::FnSig::new(self.inputs().to_owned().fold_with(folder),
589+
self.output().fold_with(folder),
590+
self.variadic())
595591
}
596592

597593
fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
598594
folder.fold_fn_sig(self)
599595
}
600596

601597
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
602-
self.inputs.visit_with(visitor) || self.output.visit_with(visitor)
598+
self.inputs().to_owned().visit_with(visitor) || self.output().visit_with(visitor)
603599
}
604600
}
605601

src/librustc/ty/sty.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,34 @@ pub struct ClosureTy<'tcx> {
563563
/// - `variadic` indicates whether this is a variadic function. (only true for foreign fns)
564564
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
565565
pub struct FnSig<'tcx> {
566-
pub inputs: Vec<Ty<'tcx>>,
567-
pub output: Ty<'tcx>,
568-
pub variadic: bool
566+
inputs: Vec<Ty<'tcx>>,
567+
output: Ty<'tcx>,
568+
variadic: bool
569+
}
570+
571+
impl<'tcx> FnSig<'tcx> {
572+
pub fn new(inputs: Vec<Ty<'tcx>>, output: Ty<'tcx>, variadic: bool) -> Self {
573+
FnSig { inputs: inputs, output: output, variadic: variadic }
574+
}
575+
576+
pub fn inputs(&self) -> &[Ty<'tcx>] {
577+
&self.inputs
578+
}
579+
580+
pub fn output(&self) -> Ty<'tcx> {
581+
self.output
582+
}
583+
584+
pub fn variadic(&self) -> bool {
585+
self.variadic
586+
}
569587
}
570588

571589
pub type PolyFnSig<'tcx> = Binder<FnSig<'tcx>>;
572590

573591
impl<'tcx> PolyFnSig<'tcx> {
574-
pub fn inputs(&self) -> ty::Binder<Vec<Ty<'tcx>>> {
575-
self.map_bound_ref(|fn_sig| fn_sig.inputs.clone())
592+
pub fn inputs<'a>(&'a self) -> Binder<&[Ty<'tcx>]> {
593+
Binder(self.0.inputs())
576594
}
577595
pub fn input(&self, index: usize) -> ty::Binder<Ty<'tcx>> {
578596
self.map_bound_ref(|fn_sig| fn_sig.inputs[index])
@@ -1244,7 +1262,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12441262

12451263
// Type accessors for substructures of types
12461264
pub fn fn_args(&self) -> ty::Binder<Vec<Ty<'tcx>>> {
1247-
self.fn_sig().inputs()
1265+
ty::Binder(self.fn_sig().inputs().skip_binder().iter().cloned().collect::<Vec<_>>())
12481266
}
12491267

12501268
pub fn fn_ret(&self) -> Binder<Ty<'tcx>> {

src/librustc/ty/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'a, 'gcx, 'tcx, H: Hasher> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tc
530530
self.hash(f.unsafety);
531531
self.hash(f.abi);
532532
self.hash(f.sig.variadic());
533-
self.hash(f.sig.inputs().skip_binder().len());
533+
self.hash(f.sig.skip_binder().inputs().len());
534534
}
535535
TyDynamic(ref data, ..) => {
536536
if let Some(p) = data.principal() {

src/librustc/ty/walk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
126126
}
127127

128128
fn push_sig_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, sig: &ty::PolyFnSig<'tcx>) {
129-
stack.push(sig.0.output);
130-
stack.extend(sig.0.inputs.iter().cloned().rev());
129+
stack.push(sig.skip_binder().output());
130+
stack.extend(sig.skip_binder().inputs().iter().cloned().rev());
131131
}

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl<'tcx> fmt::Debug for ty::InstantiatedPredicates<'tcx> {
595595
impl<'tcx> fmt::Display for ty::FnSig<'tcx> {
596596
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
597597
write!(f, "fn")?;
598-
fn_sig(f, &self.inputs, self.variadic, self.output)
598+
fn_sig(f, self.inputs(), self.variadic(), self.output())
599599
}
600600
}
601601

@@ -625,7 +625,7 @@ impl fmt::Debug for ty::RegionVid {
625625

626626
impl<'tcx> fmt::Debug for ty::FnSig<'tcx> {
627627
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
628-
write!(f, "({:?}; variadic: {})->{:?}", self.inputs, self.variadic, self.output)
628+
write!(f, "({:?}; variadic: {})->{:?}", self.inputs(), self.variadic(), self.output())
629629
}
630630
}
631631

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,8 @@ impl LateLintPass for MutableTransmutes {
10841084
let typ = cx.tcx.tables().node_id_to_type(expr.id);
10851085
match typ.sty {
10861086
ty::TyFnDef(.., ref bare_fn) if bare_fn.abi == RustIntrinsic => {
1087-
let from = bare_fn.sig.0.inputs[0];
1088-
let to = bare_fn.sig.0.output;
1087+
let from = bare_fn.sig.skip_binder().inputs()[0];
1088+
let to = bare_fn.sig.skip_binder().output();
10891089
return Some((&from.sty, &to.sty));
10901090
}
10911091
_ => (),

src/librustc_lint/types.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,16 +603,16 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
603603
}
604604

605605
let sig = cx.erase_late_bound_regions(&bare_fn.sig);
606-
if !sig.output.is_nil() {
607-
let r = self.check_type_for_ffi(cache, sig.output);
606+
if !sig.output().is_nil() {
607+
let r = self.check_type_for_ffi(cache, sig.output());
608608
match r {
609609
FfiSafe => {}
610610
_ => {
611611
return r;
612612
}
613613
}
614614
}
615-
for arg in sig.inputs {
615+
for arg in sig.inputs() {
616616
let r = self.check_type_for_ffi(cache, arg);
617617
match r {
618618
FfiSafe => {}
@@ -678,12 +678,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
678678
let sig = self.cx.tcx.item_type(def_id).fn_sig();
679679
let sig = self.cx.tcx.erase_late_bound_regions(&sig);
680680

681-
for (&input_ty, input_hir) in sig.inputs.iter().zip(&decl.inputs) {
682-
self.check_type_for_ffi_and_report_errors(input_hir.ty.span, &input_ty);
681+
for (input_ty, input_hir) in sig.inputs().iter().zip(&decl.inputs) {
682+
self.check_type_for_ffi_and_report_errors(input_hir.ty.span, input_ty);
683683
}
684684

685685
if let hir::Return(ref ret_hir) = decl.output {
686-
let ret_ty = sig.output;
686+
let ret_ty = sig.output();
687687
if !ret_ty.is_nil() {
688688
self.check_type_for_ffi_and_report_errors(ret_hir.span, ret_ty);
689689
}

src/librustc_mir/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
208208
let diverges = match ty.sty {
209209
ty::TyFnDef(_, _, ref f) | ty::TyFnPtr(ref f) => {
210210
// FIXME(canndrew): This is_never should probably be an is_uninhabited
211-
f.sig.0.output.is_never()
211+
f.sig.skip_binder().output().is_never()
212212
}
213213
_ => false
214214
};

src/librustc_mir/hair/cx/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
247247
span_bug!(expr.span, "method call has late-bound regions")
248248
});
249249

250-
assert_eq!(sig.inputs.len(), 2);
250+
assert_eq!(sig.inputs().len(), 2);
251251

252252
let tupled_args = Expr {
253-
ty: sig.inputs[1],
253+
ty: sig.inputs()[1],
254254
temp_lifetime: temp_lifetime,
255255
span: expr.span,
256256
kind: ExprKind::Tuple {

src/librustc_mir/mir_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BuildMir<'a, 'tcx> {
238238
.iter()
239239
.enumerate()
240240
.map(|(index, arg)| {
241-
(fn_sig.inputs[index], Some(&*arg.pat))
241+
(fn_sig.inputs()[index], Some(&*arg.pat))
242242
});
243243

244244
let body = self.tcx.map.expr(body_id);
245245

246246
let arguments = implicit_argument.into_iter().chain(explicit_arguments);
247247
self.cx(MirSource::Fn(id)).build(|cx| {
248-
build::construct_fn(cx, id, arguments, abi, fn_sig.output, body)
248+
build::construct_fn(cx, id, arguments, abi, fn_sig.output(), body)
249249
});
250250

251251
intravisit::walk_fn(self, fk, decl, body_id, span, id);

src/librustc_mir/transform/type_check.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,15 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
506506
match *destination {
507507
Some((ref dest, _)) => {
508508
let dest_ty = dest.ty(mir, tcx).to_ty(tcx);
509-
if let Err(terr) = self.sub_types(sig.output, dest_ty) {
509+
if let Err(terr) = self.sub_types(sig.output(), dest_ty) {
510510
span_mirbug!(self, term,
511511
"call dest mismatch ({:?} <- {:?}): {:?}",
512-
dest_ty, sig.output, terr);
512+
dest_ty, sig.output(), terr);
513513
}
514514
},
515515
None => {
516516
// FIXME(canndrew): This is_never should probably be an is_uninhabited
517-
if !sig.output.is_never() {
517+
if !sig.output().is_never() {
518518
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
519519
}
520520
},
@@ -528,11 +528,11 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
528528
args: &[Operand<'tcx>])
529529
{
530530
debug!("check_call_inputs({:?}, {:?})", sig, args);
531-
if args.len() < sig.inputs.len() ||
532-
(args.len() > sig.inputs.len() && !sig.variadic) {
531+
if args.len() < sig.inputs().len() ||
532+
(args.len() > sig.inputs().len() && !sig.variadic()) {
533533
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
534534
}
535-
for (n, (fn_arg, op_arg)) in sig.inputs.iter().zip(args).enumerate() {
535+
for (n, (fn_arg, op_arg)) in sig.inputs().iter().zip(args).enumerate() {
536536
let op_arg_ty = op_arg.ty(mir, self.tcx());
537537
if let Err(terr) = self.sub_types(op_arg_ty, fn_arg) {
538538
span_mirbug!(self, term, "bad arg #{:?} ({:?} <- {:?}): {:?}",
@@ -562,12 +562,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
562562

563563
// box_free takes a Box as a pointer. Allow for that.
564564

565-
if sig.inputs.len() != 1 {
565+
if sig.inputs().len() != 1 {
566566
span_mirbug!(self, term, "box_free should take 1 argument");
567567
return;
568568
}
569569

570-
let pointee_ty = match sig.inputs[0].sty {
570+
let pointee_ty = match sig.inputs()[0].sty {
571571
ty::TyRawPtr(mt) => mt.ty,
572572
_ => {
573573
span_mirbug!(self, term, "box_free should take a raw ptr");

0 commit comments

Comments
 (0)