Skip to content

Commit a2f303a

Browse files
committed
auto merge of #18743 : nikomatsakis/rust/hrtb-refactor-2, r=pcwalton
Various miscellaneous changes pushing towards HRTB support: 1. Update parser and adjust ast to support `for<'a,'b>` syntax, both in closures and trait bounds. Warn on the old syntax (not error, for stage0). 2. Refactor TyTrait representation to include a TraitRef. 3. Purge `once_fns` feature gate and `once` keyword. r? @pcwalton This is a [breaking-change]: - The `once_fns` feature is now officially deprecated. Rewrite using normal closures or unboxed closures. - The new `for`-based syntax now issues warnings (but not yet errors): - `fn<'a>(T) -> U` becomes `for<'a> fn(T) -> U` - `<'a> |T| -> U` becomes `for<'a> |T| -> U`
2 parents 93c85eb + cf4e53e commit a2f303a

Some content is hidden

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

52 files changed

+749
-737
lines changed

src/librustc/lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,8 @@ impl LintPass for Stability {
16901690
for t in supertraits.iter() {
16911691
match *t {
16921692
ast::TraitTyParamBound(ref t) => {
1693-
let id = ty::trait_ref_to_def_id(cx.tcx, t);
1694-
self.lint(cx, id, t.path.span);
1693+
let id = ty::trait_ref_to_def_id(cx.tcx, &t.trait_ref);
1694+
self.lint(cx, id, t.trait_ref.path.span);
16951695
}
16961696
_ => (/* pass */)
16971697
}

src/librustc/metadata/tydecode.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,10 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
389389
}
390390
'x' => {
391391
assert_eq!(next(st), '[');
392-
let def = parse_def(st, NominalType, |x,y| conv(x,y));
393-
let substs = parse_substs(st, |x,y| conv(x,y));
392+
let trait_ref = parse_trait_ref(st, |x,y| conv(x,y));
394393
let bounds = parse_existential_bounds(st, |x,y| conv(x,y));
395394
assert_eq!(next(st), ']');
396-
return ty::mk_trait(st.tcx, def, substs, bounds);
395+
return ty::mk_trait(st.tcx, trait_ref, bounds);
397396
}
398397
'p' => {
399398
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));

src/librustc/metadata/tyencode.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,10 @@ fn enc_sty(w: &mut SeekableMemWriter, cx: &ctxt, st: &ty::sty) {
231231
enc_substs(w, cx, substs);
232232
mywrite!(w, "]");
233233
}
234-
ty::ty_trait(box ty::TyTrait {
235-
def_id,
236-
ref substs,
237-
ref bounds
238-
}) => {
239-
mywrite!(w, "x[{}|", (cx.ds)(def_id));
240-
enc_substs(w, cx, substs);
234+
ty::ty_trait(box ty::TyTrait { ref principal,
235+
ref bounds }) => {
236+
mywrite!(w, "x[");
237+
enc_trait_ref(w, cx, principal);
241238
enc_existential_bounds(w, cx, bounds);
242239
mywrite!(w, "]");
243240
}

src/librustc/middle/astencode.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -1085,16 +1085,19 @@ impl<'a> rbml_writer_helpers for Encoder<'a> {
10851085
this.emit_enum_variant_arg(1, |this| idx.encode(this))
10861086
})
10871087
}
1088-
ty::UnsizeVtable(ty::TyTrait { def_id,
1089-
bounds: ref b,
1090-
ref substs },
1088+
ty::UnsizeVtable(ty::TyTrait { ref principal,
1089+
bounds: ref b },
10911090
self_ty) => {
10921091
this.emit_enum_variant("UnsizeVtable", 2, 4, |this| {
1093-
this.emit_enum_variant_arg(
1094-
0, |this| Ok(this.emit_existential_bounds(ecx, b)));
1095-
this.emit_enum_variant_arg(1, |this| def_id.encode(this));
1096-
this.emit_enum_variant_arg(2, |this| Ok(this.emit_ty(ecx, self_ty)));
1097-
this.emit_enum_variant_arg(3, |this| Ok(this.emit_substs(ecx, substs)))
1092+
this.emit_enum_variant_arg(0, |this| {
1093+
try!(this.emit_struct_field("principal", 0, |this| {
1094+
Ok(this.emit_trait_ref(ecx, &*principal))
1095+
}));
1096+
this.emit_struct_field("bounds", 1, |this| {
1097+
Ok(this.emit_existential_bounds(ecx, b))
1098+
})
1099+
});
1100+
this.emit_enum_variant_arg(1, |this| Ok(this.emit_ty(ecx, self_ty)))
10981101
})
10991102
}
11001103
}
@@ -1693,18 +1696,19 @@ impl<'a> rbml_decoder_decoder_helpers for reader::Decoder<'a> {
16931696
ty::UnsizeStruct(box uk, idx)
16941697
}
16951698
2 => {
1696-
let b =
1697-
this.read_enum_variant_arg(
1698-
0, |this| Ok(this.read_existential_bounds(dcx))).unwrap();
1699-
let def_id: ast::DefId =
1700-
this.read_enum_variant_arg(1, |this| Decodable::decode(this)).unwrap();
1699+
let ty_trait = try!(this.read_enum_variant_arg(0, |this| {
1700+
let principal = try!(this.read_struct_field("principal", 0, |this| {
1701+
Ok(this.read_trait_ref(dcx))
1702+
}));
1703+
Ok(ty::TyTrait {
1704+
principal: (*principal).clone(),
1705+
bounds: try!(this.read_struct_field("bounds", 1, |this| {
1706+
Ok(this.read_existential_bounds(dcx))
1707+
})),
1708+
})
1709+
}));
17011710
let self_ty =
1702-
this.read_enum_variant_arg(2, |this| Ok(this.read_ty(dcx))).unwrap();
1703-
let substs = this.read_enum_variant_arg(3,
1704-
|this| Ok(this.read_substs(dcx))).unwrap();
1705-
let ty_trait = ty::TyTrait { def_id: def_id.tr(dcx),
1706-
bounds: b,
1707-
substs: substs };
1711+
this.read_enum_variant_arg(1, |this| Ok(this.read_ty(dcx))).unwrap();
17081712
ty::UnsizeVtable(ty_trait, self_ty)
17091713
}
17101714
_ => panic!("bad enum variant for ty::UnsizeKind")

src/librustc/middle/privacy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
12911291
match *ty_param_bound {
12921292
ast::TraitTyParamBound(ref trait_ref) => {
12931293
if !self.tcx.sess.features.borrow().visible_private_types &&
1294-
self.path_is_private_type(trait_ref.ref_id) {
1294+
self.path_is_private_type(trait_ref.trait_ref.ref_id) {
12951295
self.tcx.sess.span_err(span,
12961296
"private type in exported type \
12971297
parameter bound");
@@ -1432,7 +1432,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14321432
//
14331433
// Those in 2. are warned via walk_generics and this
14341434
// call here.
1435-
visit::walk_trait_ref_helper(self, tr)
1435+
self.visit_trait_ref(tr)
14361436
}
14371437
}
14381438
} else if trait_ref.is_none() && self_is_public_path {

src/librustc/middle/resolve.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use syntax::ast::{ItemTrait, ItemTy, LOCAL_CRATE, Local, ItemConst};
3333
use syntax::ast::{MethodImplItem, Mod, Name, NamedField, NodeId};
3434
use syntax::ast::{Pat, PatEnum, PatIdent, PatLit};
3535
use syntax::ast::{PatRange, PatStruct, Path, PathListIdent, PathListMod};
36-
use syntax::ast::{PrimTy, Public, SelfExplicit, SelfStatic};
36+
use syntax::ast::{PolyTraitRef, PrimTy, Public, SelfExplicit, SelfStatic};
3737
use syntax::ast::{RegionTyParamBound, StmtDecl, StructField};
3838
use syntax::ast::{StructVariantKind, TraitRef, TraitTyParamBound};
3939
use syntax::ast::{TupleVariantKind, Ty, TyBool, TyChar, TyClosure, TyF32};
4040
use syntax::ast::{TyF64, TyFloat, TyI, TyI8, TyI16, TyI32, TyI64, TyInt};
41-
use syntax::ast::{TyParam, TyParamBound, TyPath, TyPtr, TyProc, TyQPath};
41+
use syntax::ast::{TyParam, TyParamBound, TyPath, TyPtr, TyPolyTraitRef, TyProc, TyQPath};
4242
use syntax::ast::{TyRptr, TyStr, TyU, TyU8, TyU16, TyU32, TyU64, TyUint};
4343
use syntax::ast::{TypeImplItem, UnnamedField};
4444
use syntax::ast::{Variant, ViewItem, ViewItemExternCrate};
@@ -607,6 +607,7 @@ enum TraitReferenceType {
607607
TraitImplementation, // impl SomeTrait for T { ... }
608608
TraitDerivation, // trait T : SomeTrait { ... }
609609
TraitBoundingTypeParameter, // fn f<T:SomeTrait>() { ... }
610+
TraitObject, // Box<for<'a> SomeTrait>
610611
}
611612

612613
impl NameBindings {
@@ -4244,11 +4245,11 @@ impl<'a> Resolver<'a> {
42444245
this.resolve_type_parameter_bounds(item.id, bounds,
42454246
TraitDerivation);
42464247

4247-
match unbound {
4248-
&Some(ast::TraitTyParamBound(ref tpb)) => {
4248+
match *unbound {
4249+
Some(ref tpb) => {
42494250
this.resolve_trait_reference(item.id, tpb, TraitDerivation);
42504251
}
4251-
_ => {}
4252+
None => {}
42524253
}
42534254

42544255
for trait_item in (*trait_items).iter() {
@@ -4495,7 +4496,7 @@ impl<'a> Resolver<'a> {
44954496
}
44964497
match &type_parameter.unbound {
44974498
&Some(ref unbound) =>
4498-
self.resolve_type_parameter_bound(
4499+
self.resolve_trait_reference(
44994500
type_parameter.id, unbound, TraitBoundingTypeParameter),
45004501
&None => {}
45014502
}
@@ -4521,12 +4522,19 @@ impl<'a> Resolver<'a> {
45214522
reference_type: TraitReferenceType) {
45224523
match *type_parameter_bound {
45234524
TraitTyParamBound(ref tref) => {
4524-
self.resolve_trait_reference(id, tref, reference_type)
4525+
self.resolve_poly_trait_reference(id, tref, reference_type)
45254526
}
45264527
RegionTyParamBound(..) => {}
45274528
}
45284529
}
45294530

4531+
fn resolve_poly_trait_reference(&mut self,
4532+
id: NodeId,
4533+
poly_trait_reference: &PolyTraitRef,
4534+
reference_type: TraitReferenceType) {
4535+
self.resolve_trait_reference(id, &poly_trait_reference.trait_ref, reference_type)
4536+
}
4537+
45304538
fn resolve_trait_reference(&mut self,
45314539
id: NodeId,
45324540
trait_reference: &TraitRef,
@@ -4538,6 +4546,7 @@ impl<'a> Resolver<'a> {
45384546
TraitBoundingTypeParameter => "bound type parameter with",
45394547
TraitImplementation => "implement",
45404548
TraitDerivation => "derive",
4549+
TraitObject => "reference",
45414550
};
45424551

45434552
let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str);
@@ -5044,6 +5053,13 @@ impl<'a> Resolver<'a> {
50445053
visit::walk_ty(self, ty);
50455054
}
50465055

5056+
TyPolyTraitRef(ref poly_trait_ref) => {
5057+
self.resolve_poly_trait_reference(
5058+
ty.id,
5059+
&**poly_trait_ref,
5060+
TraitObject);
5061+
visit::walk_ty(self, ty);
5062+
}
50475063
_ => {
50485064
// Just resolve embedded types.
50495065
visit::walk_ty(self, ty);

0 commit comments

Comments
 (0)