Skip to content

Commit

Permalink
auto merge of rust-lang#15521 : nick29581/rust/type, r=pcwalton
Browse files Browse the repository at this point in the history
closes rust-lang#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) {}
```
  • Loading branch information
bors committed Jul 8, 2014
2 parents bfe4ddf + a0cfda5 commit 35e2134
Show file tree
Hide file tree
Showing 29 changed files with 215 additions and 178 deletions.
4 changes: 2 additions & 2 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ fn fold_item_underscore(cx: &mut Context, item: &ast::Item_) -> ast::Item_ {
.map(|x| *x).collect();
ast::ItemImpl((*a).clone(), (*b).clone(), c, methods)
}
ast::ItemTrait(ref a, b, ref c, ref methods) => {
ast::ItemTrait(ref a, ref b, ref c, ref methods) => {
let methods = methods.iter()
.filter(|m| trait_method_in_cfg(cx, *m) )
.map(|x| (*x).clone())
.collect();
ast::ItemTrait((*a).clone(), b, (*c).clone(), methods)
ast::ItemTrait((*a).clone(), (*b).clone(), (*c).clone(), methods)
}
ast::ItemStruct(ref def, ref generics) => {
ast::ItemStruct(fold_struct(cx, &**def), generics.clone())
Expand Down
20 changes: 0 additions & 20 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,6 @@ fn item_visibility(item: ebml::Doc) -> ast::Visibility {
}
}

fn item_sized(item: ebml::Doc) -> ast::Sized {
match reader::maybe_get_doc(item, tag_items_data_item_sized) {
None => ast::StaticSize,
Some(sized_doc) => {
match reader::doc_as_u8(sized_doc) as char {
'd' => ast::DynSize,
's' => ast::StaticSize,
_ => fail!("unknown sized-ness character")
}
}
}
}

fn item_method_sort(item: ebml::Doc) -> char {
let mut ret = 'r';
reader::tagged_docs(item, tag_item_trait_method_sort, |doc| {
Expand Down Expand Up @@ -393,7 +380,6 @@ pub fn get_trait_def(cdata: Cmd,
let tp_defs = item_ty_param_defs(item_doc, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let rp_defs = item_region_param_defs(item_doc, cdata);
let sized = item_sized(item_doc);
let mut bounds = ty::empty_builtin_bounds();
// Collect the builtin bounds from the encoded supertraits.
// FIXME(#8559): They should be encoded directly.
Expand All @@ -405,12 +391,6 @@ pub fn get_trait_def(cdata: Cmd,
});
true
});
// Turn sized into a bound, FIXME(#8559).
if sized == ast::StaticSize {
tcx.lang_items.to_builtin_kind(tcx.lang_items.sized_trait().unwrap()).map(|bound| {
bounds.add(bound);
});
}

ty::TraitDef {
generics: ty::Generics {types: tp_defs,
Expand Down
15 changes: 1 addition & 14 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,16 +881,6 @@ fn encode_extension_implementations(ecx: &EncodeContext,
}
}

fn encode_sized(ebml_w: &mut Encoder, sized: Sized) {
ebml_w.start_tag(tag_items_data_item_sized);
let ch = match sized {
DynSize => 'd',
StaticSize => 's',
};
ebml_w.wr_str(str::from_char(ch).as_slice());
ebml_w.end_tag();
}

fn encode_stability(ebml_w: &mut Encoder, stab_opt: Option<attr::Stability>) {
stab_opt.map(|stab| {
ebml_w.start_tag(tag_items_data_item_stability);
Expand Down Expand Up @@ -1149,7 +1139,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
ast_method)
}
}
ItemTrait(_, sized, ref super_traits, ref ms) => {
ItemTrait(_, _, ref super_traits, ref ms) => {
add_to_index(item, ebml_w, index);
ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, def_id);
Expand All @@ -1163,9 +1153,6 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_trait_ref(ebml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref);
encode_name(ebml_w, item.ident.name);
encode_attributes(ebml_w, item.attrs.as_slice());
// When we fix the rest of the supertrait nastiness (FIXME(#8559)), we
// should no longer need this ugly little hack either.
encode_sized(ebml_w, sized);
encode_visibility(ebml_w, vis);
encode_stability(ebml_w, stab);
for &method_def_id in ty::trait_method_def_ids(tcx, def_id).iter() {
Expand Down
21 changes: 16 additions & 5 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3598,6 +3598,7 @@ impl<'a> Resolver<'a> {
item.id,
ItemRibKind),
|this| {
this.resolve_type_parameters(&generics.ty_params);
visit::walk_item(this, item, ());
});
}
Expand All @@ -3623,7 +3624,7 @@ impl<'a> Resolver<'a> {
methods.as_slice());
}

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

Expand All @@ -3645,6 +3646,12 @@ impl<'a> Resolver<'a> {
for trt in traits.iter() {
this.resolve_trait_reference(item.id, trt, TraitDerivation);
}
match unbound {
&Some(ast::TraitTyParamBound(ref tpb)) => {
this.resolve_trait_reference(item.id, tpb, TraitDerivation);
}
_ => {}
}

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

fn resolve_type_parameters(&mut self,
type_parameters: &OwnedSlice<TyParam>) {
type_parameters: &OwnedSlice<TyParam>) {
for type_parameter in type_parameters.iter() {
for bound in type_parameter.bounds.iter() {
self.resolve_type_parameter_bound(type_parameter.id, bound);
}
match &type_parameter.unbound {
&Some(ref unbound) => self.resolve_type_parameter_bound(type_parameter.id, unbound),
&None => {}
}
match type_parameter.default {
Some(ref ty) => self.resolve_type(&**ty),
None => {}
Expand All @@ -3887,9 +3898,9 @@ impl<'a> Resolver<'a> {
}

fn resolve_trait_reference(&mut self,
id: NodeId,
trait_reference: &TraitRef,
reference_type: TraitReferenceType) {
id: NodeId,
trait_reference: &TraitRef,
reference_type: TraitReferenceType) {
match self.resolve_path(id, &trait_reference.path, TypeNS, true) {
None => {
let path_str = self.path_idents_to_str(&trait_reference.path);
Expand Down
75 changes: 50 additions & 25 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
_ => {}
}

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

let builtin_bounds =
ensure_supertraits(ccx, it.id, it.span, supertraits, sized);
ensure_supertraits(ccx, it.id, it.span, supertraits, unbound);

let substs = mk_item_substs(ccx, &ty_generics);
let trait_def = Rc::new(ty::TraitDef {
Expand Down Expand Up @@ -759,7 +759,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
id: ast::NodeId,
sp: codemap::Span,
ast_trait_refs: &Vec<ast::TraitRef>,
sized: ast::Sized)
unbound: &Option<ast::TyParamBound>)
-> ty::BuiltinBounds
{
let tcx = ccx.tcx;
Expand Down Expand Up @@ -798,15 +798,7 @@ pub fn trait_def_of_item(ccx: &CrateCtxt, it: &ast::Item) -> Rc<ty::TraitDef> {
}
}

if sized == ast::StaticSize {
match tcx.lang_items.require(SizedTraitLangItem) {
Ok(def_id) => {
ty::try_add_builtin_trait(tcx, def_id, &mut bounds);
}
Err(s) => tcx.sess.err(s.as_slice()),
};
}

add_unsized_bound(ccx, unbound, &mut bounds, "trait", sp);
tcx.supertraits.borrow_mut().insert(local_def(id),
Rc::new(ty_trait_refs));
bounds
Expand Down Expand Up @@ -974,6 +966,43 @@ fn ty_generics_for_fn_or_method(ccx: &CrateCtxt,
&generics.ty_params, base_generics)
}

// Add the Sized bound, unless the type parameter is marked as `Sized?`.
fn add_unsized_bound(ccx: &CrateCtxt,
unbound: &Option<ast::TyParamBound>,
bounds: &mut ty::BuiltinBounds,
desc: &str,
span: Span) {
let kind_id = ccx.tcx.lang_items.require(SizedTraitLangItem);
match unbound {
&Some(TraitTyParamBound(ref tpb)) => {
// #FIXME(8559) currently requires the unbound to be built-in.
let trait_def_id = ty::trait_ref_to_def_id(ccx.tcx, tpb);
match kind_id {
Ok(kind_id) if trait_def_id != kind_id => {
ccx.tcx.sess.span_warn(span,
format!("default bound relaxed \
for a {}, but this does \
nothing because the given \
bound is not a default. \
Only `Sized?` is supported.",
desc).as_slice());
ty::try_add_builtin_trait(ccx.tcx,
kind_id,
bounds);
}
_ => {}
}
}
_ if kind_id.is_ok() => {
ty::try_add_builtin_trait(ccx.tcx,
kind_id.unwrap(),
bounds);
}
// No lang item for Sized, so we can't add it as a bound.
_ => {}
}
}

fn ty_generics(ccx: &CrateCtxt,
space: subst::ParamSpace,
lifetimes: &Vec<ast::Lifetime>,
Expand Down Expand Up @@ -1016,7 +1045,7 @@ fn ty_generics(ccx: &CrateCtxt,
let bounds = Rc::new(compute_bounds(ccx,
param_ty,
&param.bounds,
param.sized,
&param.unbound,
param.ident,
param.span));
let default = param.default.map(|path| {
Expand Down Expand Up @@ -1056,7 +1085,7 @@ fn ty_generics(ccx: &CrateCtxt,
ccx: &CrateCtxt,
param_ty: ty::ParamTy,
ast_bounds: &OwnedSlice<ast::TyParamBound>,
sized: ast::Sized,
unbound: &Option<ast::TyParamBound>,
ident: ast::Ident,
span: Span) -> ty::ParamBounds
{
Expand Down Expand Up @@ -1113,15 +1142,11 @@ fn ty_generics(ccx: &CrateCtxt,
}
}

if sized == ast::StaticSize {
match ccx.tcx.lang_items.require(SizedTraitLangItem) {
Ok(def_id) => { ty::try_add_builtin_trait(ccx.tcx,
def_id,
&mut param_bounds.builtin_bounds); },
// Fixme(13367) after `type` makes it into the snapshot, we can check this properly
Err(_s) => {}, //ccx.tcx.sess.err(s),
}
}
add_unsized_bound(ccx,
unbound,
&mut param_bounds.builtin_bounds,
"type parameter",
span);

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,9 @@ impl<'a> Rebuilder<'a> {
ident: ty_param.ident,
id: ty_param.id,
bounds: bounds,
unbound: ty_param.unbound.clone(),
default: ty_param.default,
span: ty_param.span,
sized: ty_param.sized,
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
// miscellaneous, no highlighting
t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI |
t::COLON | t::MOD_SEP | t::LARROW | t::LPAREN |
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE => "",
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE | t::QUESTION => "",
t::DOLLAR => {
if t::is_ident(&lexer.peek().tok) {
is_macro_nonterminal = true;
Expand Down
14 changes: 6 additions & 8 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ pub enum TyParamBound {
pub struct TyParam {
pub ident: Ident,
pub id: NodeId,
pub sized: Sized,
pub bounds: OwnedSlice<TyParamBound>,
pub unbound: Option<TyParamBound>,
pub default: Option<P<Ty>>,
pub span: Span
}
Expand Down Expand Up @@ -1041,12 +1041,6 @@ impl Visibility {
}
}

#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
pub enum Sized {
DynSize,
StaticSize,
}

#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
pub struct StructField_ {
pub kind: StructFieldKind,
Expand Down Expand Up @@ -1115,7 +1109,11 @@ pub enum Item_ {
ItemEnum(EnumDef, Generics),
ItemStruct(Gc<StructDef>, Generics),
/// Represents a Trait Declaration
ItemTrait(Generics, Sized, Vec<TraitRef> , Vec<TraitMethod> ),
ItemTrait(Generics,
Option<TyParamBound>, // (optional) default bound not required for Self.
// Currently, only Sized makes sense here.
Vec<TraitRef> ,
Vec<TraitMethod>),
ItemImpl(Generics,
Option<TraitRef>, // (optional) trait this impl implements
P<Ty>, // self
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub trait AstBuilder {
fn typaram(&self,
span: Span,
id: ast::Ident,
sized: ast::Sized,
bounds: OwnedSlice<ast::TyParamBound>,
unbound: Option<ast::TyParamBound>,
default: Option<P<ast::Ty>>) -> ast::TyParam;

fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
Expand Down Expand Up @@ -396,14 +396,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn typaram(&self,
span: Span,
id: ast::Ident,
sized: ast::Sized,
bounds: OwnedSlice<ast::TyParamBound>,
unbound: Option<ast::TyParamBound>,
default: Option<P<ast::Ty>>) -> ast::TyParam {
ast::TyParam {
ident: id,
id: ast::DUMMY_NODE_ID,
sized: sized,
bounds: bounds,
unbound: unbound,
default: default,
span: span
}
Expand All @@ -423,7 +423,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {

fn strip_bounds(&self, generics: &Generics) -> Generics {
let new_params = generics.ty_params.map(|ty_param| {
ast::TyParam { bounds: OwnedSlice::empty(), ..*ty_param }
ast::TyParam { bounds: OwnedSlice::empty(), unbound: None, ..*ty_param }
});
Generics {
ty_params: new_params,
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ The compiler code necessary for `#[deriving(Decodable)]`. See
encodable.rs for more.
*/

use ast;
use ast::{MetaItem, Item, Expr, MutMutable, Ident};
use codemap::Span;
use ext::base::ExtCtxt;
Expand All @@ -39,10 +38,10 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
additional_bounds: Vec::new(),
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__D", ast::StaticSize, vec!(Path::new_(
bounds: vec!(("__D", None, vec!(Path::new_(
vec!("serialize", "Decoder"), None,
vec!(box Literal(Path::new_local("__E"))), true))),
("__E", ast::StaticSize, vec!()))
("__E", None, vec!()))
},
methods: vec!(
MethodDef {
Expand Down
Loading

0 comments on commit 35e2134

Please sign in to comment.