Skip to content

Commit 35e2134

Browse files
committed
auto merge of #15521 : nick29581/rust/type, r=pcwalton
closes #13367 [breaking-change] Use `Sized?` to indicate a dynamically sized type parameter or trait (used to be `type`). E.g., ``` trait Tr for Sized? {} fn foo<Sized? X: Share>(x: X) {} ```
2 parents bfe4ddf + a0cfda5 commit 35e2134

29 files changed

+215
-178
lines changed

src/librustc/front/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ fn fold_item_underscore(cx: &mut Context, item: &ast::Item_) -> ast::Item_ {
109109
.map(|x| *x).collect();
110110
ast::ItemImpl((*a).clone(), (*b).clone(), c, methods)
111111
}
112-
ast::ItemTrait(ref a, b, ref c, ref methods) => {
112+
ast::ItemTrait(ref a, ref b, ref c, ref methods) => {
113113
let methods = methods.iter()
114114
.filter(|m| trait_method_in_cfg(cx, *m) )
115115
.map(|x| (*x).clone())
116116
.collect();
117-
ast::ItemTrait((*a).clone(), b, (*c).clone(), methods)
117+
ast::ItemTrait((*a).clone(), (*b).clone(), (*c).clone(), methods)
118118
}
119119
ast::ItemStruct(ref def, ref generics) => {
120120
ast::ItemStruct(fold_struct(cx, &**def), generics.clone())

src/librustc/metadata/decoder.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,6 @@ fn item_visibility(item: ebml::Doc) -> ast::Visibility {
164164
}
165165
}
166166

167-
fn item_sized(item: ebml::Doc) -> ast::Sized {
168-
match reader::maybe_get_doc(item, tag_items_data_item_sized) {
169-
None => ast::StaticSize,
170-
Some(sized_doc) => {
171-
match reader::doc_as_u8(sized_doc) as char {
172-
'd' => ast::DynSize,
173-
's' => ast::StaticSize,
174-
_ => fail!("unknown sized-ness character")
175-
}
176-
}
177-
}
178-
}
179-
180167
fn item_method_sort(item: ebml::Doc) -> char {
181168
let mut ret = 'r';
182169
reader::tagged_docs(item, tag_item_trait_method_sort, |doc| {
@@ -393,7 +380,6 @@ pub fn get_trait_def(cdata: Cmd,
393380
let tp_defs = item_ty_param_defs(item_doc, tcx, cdata,
394381
tag_items_data_item_ty_param_bounds);
395382
let rp_defs = item_region_param_defs(item_doc, cdata);
396-
let sized = item_sized(item_doc);
397383
let mut bounds = ty::empty_builtin_bounds();
398384
// Collect the builtin bounds from the encoded supertraits.
399385
// FIXME(#8559): They should be encoded directly.
@@ -405,12 +391,6 @@ pub fn get_trait_def(cdata: Cmd,
405391
});
406392
true
407393
});
408-
// Turn sized into a bound, FIXME(#8559).
409-
if sized == ast::StaticSize {
410-
tcx.lang_items.to_builtin_kind(tcx.lang_items.sized_trait().unwrap()).map(|bound| {
411-
bounds.add(bound);
412-
});
413-
}
414394

415395
ty::TraitDef {
416396
generics: ty::Generics {types: tp_defs,

src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -881,16 +881,6 @@ fn encode_extension_implementations(ecx: &EncodeContext,
881881
}
882882
}
883883

884-
fn encode_sized(ebml_w: &mut Encoder, sized: Sized) {
885-
ebml_w.start_tag(tag_items_data_item_sized);
886-
let ch = match sized {
887-
DynSize => 'd',
888-
StaticSize => 's',
889-
};
890-
ebml_w.wr_str(str::from_char(ch).as_slice());
891-
ebml_w.end_tag();
892-
}
893-
894884
fn encode_stability(ebml_w: &mut Encoder, stab_opt: Option<attr::Stability>) {
895885
stab_opt.map(|stab| {
896886
ebml_w.start_tag(tag_items_data_item_stability);
@@ -1149,7 +1139,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11491139
ast_method)
11501140
}
11511141
}
1152-
ItemTrait(_, sized, ref super_traits, ref ms) => {
1142+
ItemTrait(_, _, ref super_traits, ref ms) => {
11531143
add_to_index(item, ebml_w, index);
11541144
ebml_w.start_tag(tag_items_data_item);
11551145
encode_def_id(ebml_w, def_id);
@@ -1163,9 +1153,6 @@ fn encode_info_for_item(ecx: &EncodeContext,
11631153
encode_trait_ref(ebml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref);
11641154
encode_name(ebml_w, item.ident.name);
11651155
encode_attributes(ebml_w, item.attrs.as_slice());
1166-
// When we fix the rest of the supertrait nastiness (FIXME(#8559)), we
1167-
// should no longer need this ugly little hack either.
1168-
encode_sized(ebml_w, sized);
11691156
encode_visibility(ebml_w, vis);
11701157
encode_stability(ebml_w, stab);
11711158
for &method_def_id in ty::trait_method_def_ids(tcx, def_id).iter() {

src/librustc/middle/resolve.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,6 +3598,7 @@ impl<'a> Resolver<'a> {
35983598
item.id,
35993599
ItemRibKind),
36003600
|this| {
3601+
this.resolve_type_parameters(&generics.ty_params);
36013602
visit::walk_item(this, item, ());
36023603
});
36033604
}
@@ -3623,7 +3624,7 @@ impl<'a> Resolver<'a> {
36233624
methods.as_slice());
36243625
}
36253626

3626-
ItemTrait(ref generics, _, ref traits, ref methods) => {
3627+
ItemTrait(ref generics, ref unbound, ref traits, ref methods) => {
36273628
// Create a new rib for the self type.
36283629
let self_type_rib = Rib::new(ItemRibKind);
36293630

@@ -3645,6 +3646,12 @@ impl<'a> Resolver<'a> {
36453646
for trt in traits.iter() {
36463647
this.resolve_trait_reference(item.id, trt, TraitDerivation);
36473648
}
3649+
match unbound {
3650+
&Some(ast::TraitTyParamBound(ref tpb)) => {
3651+
this.resolve_trait_reference(item.id, tpb, TraitDerivation);
3652+
}
3653+
_ => {}
3654+
}
36483655

36493656
for method in (*methods).iter() {
36503657
// Create a new rib for the method-specific type
@@ -3856,11 +3863,15 @@ impl<'a> Resolver<'a> {
38563863
}
38573864

38583865
fn resolve_type_parameters(&mut self,
3859-
type_parameters: &OwnedSlice<TyParam>) {
3866+
type_parameters: &OwnedSlice<TyParam>) {
38603867
for type_parameter in type_parameters.iter() {
38613868
for bound in type_parameter.bounds.iter() {
38623869
self.resolve_type_parameter_bound(type_parameter.id, bound);
38633870
}
3871+
match &type_parameter.unbound {
3872+
&Some(ref unbound) => self.resolve_type_parameter_bound(type_parameter.id, unbound),
3873+
&None => {}
3874+
}
38643875
match type_parameter.default {
38653876
Some(ref ty) => self.resolve_type(&**ty),
38663877
None => {}
@@ -3887,9 +3898,9 @@ impl<'a> Resolver<'a> {
38873898
}
38883899

38893900
fn resolve_trait_reference(&mut self,
3890-
id: NodeId,
3891-
trait_reference: &TraitRef,
3892-
reference_type: TraitReferenceType) {
3901+
id: NodeId,
3902+
trait_reference: &TraitRef,
3903+
reference_type: TraitReferenceType) {
38933904
match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
38943905
None => {
38953906
let path_str = self.path_idents_to_str(&trait_reference.path);

src/librustc/middle/typeck/collect.rs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,9 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
692692
_ => {}
693693
}
694694

695-
let (generics, sized, supertraits) = match it.node {
696-
ast::ItemTrait(ref generics, sized, ref supertraits, _) => {
697-
(generics, sized, supertraits)
695+
let (generics, unbound, supertraits) = match it.node {
696+
ast::ItemTrait(ref generics, ref unbound, ref supertraits, _) => {
697+
(generics, unbound, supertraits)
698698
}
699699
ref s => {
700700
tcx.sess.span_bug(
@@ -711,7 +711,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
711711
generics);
712712

713713
let builtin_bounds =
714-
ensure_supertraits(ccx, it.id, it.span, supertraits, sized);
714+
ensure_supertraits(ccx, it.id, it.span, supertraits, unbound);
715715

716716
let substs = mk_item_substs(ccx, &ty_generics);
717717
let trait_def = Rc::new(ty::TraitDef {
@@ -759,7 +759,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
759759
id: ast::NodeId,
760760
sp: codemap::Span,
761761
ast_trait_refs: &Vec<ast::TraitRef>,
762-
sized: ast::Sized)
762+
unbound: &Option<ast::TyParamBound>)
763763
-> ty::BuiltinBounds
764764
{
765765
let tcx = ccx.tcx;
@@ -798,15 +798,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
798798
}
799799
}
800800

801-
if sized == ast::StaticSize {
802-
match tcx.lang_items.require(SizedTraitLangItem) {
803-
Ok(def_id) => {
804-
ty::try_add_builtin_trait(tcx, def_id, &mut bounds);
805-
}
806-
Err(s) => tcx.sess.err(s.as_slice()),
807-
};
808-
}
809-
801+
add_unsized_bound(ccx, unbound, &mut bounds, "trait", sp);
810802
tcx.supertraits.borrow_mut().insert(local_def(id),
811803
Rc::new(ty_trait_refs));
812804
bounds
@@ -974,6 +966,43 @@ fn ty_generics_for_fn_or_method(ccx: &CrateCtxt,
974966
&generics.ty_params, base_generics)
975967
}
976968

969+
// Add the Sized bound, unless the type parameter is marked as `Sized?`.
970+
fn add_unsized_bound(ccx: &CrateCtxt,
971+
unbound: &Option<ast::TyParamBound>,
972+
bounds: &mut ty::BuiltinBounds,
973+
desc: &str,
974+
span: Span) {
975+
let kind_id = ccx.tcx.lang_items.require(SizedTraitLangItem);
976+
match unbound {
977+
&Some(TraitTyParamBound(ref tpb)) => {
978+
// #FIXME(8559) currently requires the unbound to be built-in.
979+
let trait_def_id = ty::trait_ref_to_def_id(ccx.tcx, tpb);
980+
match kind_id {
981+
Ok(kind_id) if trait_def_id != kind_id => {
982+
ccx.tcx.sess.span_warn(span,
983+
format!("default bound relaxed \
984+
for a {}, but this does \
985+
nothing because the given \
986+
bound is not a default. \
987+
Only `Sized?` is supported.",
988+
desc).as_slice());
989+
ty::try_add_builtin_trait(ccx.tcx,
990+
kind_id,
991+
bounds);
992+
}
993+
_ => {}
994+
}
995+
}
996+
_ if kind_id.is_ok() => {
997+
ty::try_add_builtin_trait(ccx.tcx,
998+
kind_id.unwrap(),
999+
bounds);
1000+
}
1001+
// No lang item for Sized, so we can't add it as a bound.
1002+
_ => {}
1003+
}
1004+
}
1005+
9771006
fn ty_generics(ccx: &CrateCtxt,
9781007
space: subst::ParamSpace,
9791008
lifetimes: &Vec<ast::Lifetime>,
@@ -1016,7 +1045,7 @@ fn ty_generics(ccx: &CrateCtxt,
10161045
let bounds = Rc::new(compute_bounds(ccx,
10171046
param_ty,
10181047
&param.bounds,
1019-
param.sized,
1048+
&param.unbound,
10201049
param.ident,
10211050
param.span));
10221051
let default = param.default.map(|path| {
@@ -1056,7 +1085,7 @@ fn ty_generics(ccx: &CrateCtxt,
10561085
ccx: &CrateCtxt,
10571086
param_ty: ty::ParamTy,
10581087
ast_bounds: &OwnedSlice<ast::TyParamBound>,
1059-
sized: ast::Sized,
1088+
unbound: &Option<ast::TyParamBound>,
10601089
ident: ast::Ident,
10611090
span: Span) -> ty::ParamBounds
10621091
{
@@ -1113,15 +1142,11 @@ fn ty_generics(ccx: &CrateCtxt,
11131142
}
11141143
}
11151144

1116-
if sized == ast::StaticSize {
1117-
match ccx.tcx.lang_items.require(SizedTraitLangItem) {
1118-
Ok(def_id) => { ty::try_add_builtin_trait(ccx.tcx,
1119-
def_id,
1120-
&mut param_bounds.builtin_bounds); },
1121-
// Fixme(13367) after `type` makes it into the snapshot, we can check this properly
1122-
Err(_s) => {}, //ccx.tcx.sess.err(s),
1123-
}
1124-
}
1145+
add_unsized_bound(ccx,
1146+
unbound,
1147+
&mut param_bounds.builtin_bounds,
1148+
"type parameter",
1149+
span);
11251150

11261151
check_bounds_compatible(ccx.tcx, &param_bounds, ident, span);
11271152

src/librustc/middle/typeck/infer/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,9 @@ impl<'a> Rebuilder<'a> {
895895
ident: ty_param.ident,
896896
id: ty_param.id,
897897
bounds: bounds,
898+
unbound: ty_param.unbound.clone(),
898899
default: ty_param.default,
899900
span: ty_param.span,
900-
sized: ty_param.sized,
901901
}
902902
})
903903
}

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
110110
// miscellaneous, no highlighting
111111
t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI |
112112
t::COLON | t::MOD_SEP | t::LARROW | t::LPAREN |
113-
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE => "",
113+
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE | t::QUESTION => "",
114114
t::DOLLAR => {
115115
if t::is_ident(&lexer.peek().tok) {
116116
is_macro_nonterminal = true;

src/libsyntax/ast.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ pub enum TyParamBound {
184184
pub struct TyParam {
185185
pub ident: Ident,
186186
pub id: NodeId,
187-
pub sized: Sized,
188187
pub bounds: OwnedSlice<TyParamBound>,
188+
pub unbound: Option<TyParamBound>,
189189
pub default: Option<P<Ty>>,
190190
pub span: Span
191191
}
@@ -1041,12 +1041,6 @@ impl Visibility {
10411041
}
10421042
}
10431043

1044-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
1045-
pub enum Sized {
1046-
DynSize,
1047-
StaticSize,
1048-
}
1049-
10501044
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
10511045
pub struct StructField_ {
10521046
pub kind: StructFieldKind,
@@ -1115,7 +1109,11 @@ pub enum Item_ {
11151109
ItemEnum(EnumDef, Generics),
11161110
ItemStruct(Gc<StructDef>, Generics),
11171111
/// Represents a Trait Declaration
1118-
ItemTrait(Generics, Sized, Vec<TraitRef> , Vec<TraitMethod> ),
1112+
ItemTrait(Generics,
1113+
Option<TyParamBound>, // (optional) default bound not required for Self.
1114+
// Currently, only Sized makes sense here.
1115+
Vec<TraitRef> ,
1116+
Vec<TraitMethod>),
11191117
ItemImpl(Generics,
11201118
Option<TraitRef>, // (optional) trait this impl implements
11211119
P<Ty>, // self

src/libsyntax/ext/build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ pub trait AstBuilder {
6666
fn typaram(&self,
6767
span: Span,
6868
id: ast::Ident,
69-
sized: ast::Sized,
7069
bounds: OwnedSlice<ast::TyParamBound>,
70+
unbound: Option<ast::TyParamBound>,
7171
default: Option<P<ast::Ty>>) -> ast::TyParam;
7272

7373
fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
@@ -396,14 +396,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
396396
fn typaram(&self,
397397
span: Span,
398398
id: ast::Ident,
399-
sized: ast::Sized,
400399
bounds: OwnedSlice<ast::TyParamBound>,
400+
unbound: Option<ast::TyParamBound>,
401401
default: Option<P<ast::Ty>>) -> ast::TyParam {
402402
ast::TyParam {
403403
ident: id,
404404
id: ast::DUMMY_NODE_ID,
405-
sized: sized,
406405
bounds: bounds,
406+
unbound: unbound,
407407
default: default,
408408
span: span
409409
}
@@ -423,7 +423,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
423423

424424
fn strip_bounds(&self, generics: &Generics) -> Generics {
425425
let new_params = generics.ty_params.map(|ty_param| {
426-
ast::TyParam { bounds: OwnedSlice::empty(), ..*ty_param }
426+
ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
427427
});
428428
Generics {
429429
ty_params: new_params,

src/libsyntax/ext/deriving/decodable.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ The compiler code necessary for `#[deriving(Decodable)]`. See
1313
encodable.rs for more.
1414
*/
1515

16-
use ast;
1716
use ast::{MetaItem, Item, Expr, MutMutable, Ident};
1817
use codemap::Span;
1918
use ext::base::ExtCtxt;
@@ -39,10 +38,10 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
3938
additional_bounds: Vec::new(),
4039
generics: LifetimeBounds {
4140
lifetimes: Vec::new(),
42-
bounds: vec!(("__D", ast::StaticSize, vec!(Path::new_(
41+
bounds: vec!(("__D", None, vec!(Path::new_(
4342
vec!("serialize", "Decoder"), None,
4443
vec!(box Literal(Path::new_local("__E"))), true))),
45-
("__E", ast::StaticSize, vec!()))
44+
("__E", None, vec!()))
4645
},
4746
methods: vec!(
4847
MethodDef {

0 commit comments

Comments
 (0)