Skip to content

Commit c523d86

Browse files
committed
auto merge of #15585 : bgamari/rust/subst-bug, r=pnkfelix
This branch has a fix for #15557 (a2bcef9) as well as a variety of patches I found useful while debugging this issue. These include adding `Show` impls to a variety of types, including the majority of `syntax::ast` and some of `middle::ty`.
2 parents afbcbbc + 446f937 commit c523d86

File tree

11 files changed

+181
-108
lines changed

11 files changed

+181
-108
lines changed

src/librustc/middle/def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use syntax::ast_util::local_def;
1414

1515
use std::gc::Gc;
1616

17-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
17+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1818
pub enum Def {
1919
DefFn(ast::DefId, ast::FnStyle),
2020
DefStaticMethod(/* method */ ast::DefId, MethodProvenance, ast::FnStyle),
@@ -51,7 +51,7 @@ pub enum Def {
5151
DefMethod(ast::DefId /* method */, Option<ast::DefId> /* trait */),
5252
}
5353

54-
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)]
54+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
5555
pub enum MethodProvenance {
5656
FromTrait(ast::DefId),
5757
FromImpl(ast::DefId),

src/librustc/middle/kind.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ fn check_struct_safe_for_destructor(cx: &mut Context,
8787
span: Span,
8888
struct_did: DefId) {
8989
let struct_tpt = ty::lookup_item_type(cx.tcx, struct_did);
90-
if !struct_tpt.generics.has_type_params(subst::TypeSpace) {
90+
if !struct_tpt.generics.has_type_params(subst::TypeSpace)
91+
&& !struct_tpt.generics.has_region_params(subst::TypeSpace) {
9192
let struct_ty = ty::mk_struct(cx.tcx, struct_did,
9293
subst::Substs::empty());
9394
if !ty::type_is_sendable(cx.tcx, struct_ty) {
@@ -121,7 +122,7 @@ fn check_impl_of_trait(cx: &mut Context, it: &Item, trait_ref: &TraitRef, self_t
121122

122123
// If this trait has builtin-kind supertraits, meet them.
123124
let self_ty: ty::t = ty::node_id_to_type(cx.tcx, it.id);
124-
debug!("checking impl with self type {:?}", ty::get(self_ty).sty);
125+
debug!("checking impl with self type {}", ty::get(self_ty).sty);
125126
check_builtin_bounds(cx, self_ty, trait_def.bounds, |missing| {
126127
cx.tcx.sess.span_err(self_type.span,
127128
format!("the type `{}', which does not fulfill `{}`, cannot implement this \

src/librustc/middle/subst.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use middle::ty_fold;
1515
use middle::ty_fold::{TypeFoldable, TypeFolder};
1616
use util::ppaux::Repr;
1717

18+
use std::fmt;
1819
use std::mem;
1920
use std::raw;
2021
use std::slice::{Items, MutItems};
@@ -83,7 +84,7 @@ impl<T> HomogeneousTuple3<T> for (T, T, T) {
8384
* space* (which indices where the parameter is defined; see
8485
* `ParamSpace`).
8586
*/
86-
#[deriving(Clone, PartialEq, Eq, Hash)]
87+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
8788
pub struct Substs {
8889
pub types: VecPerParamSpace<ty::t>,
8990
pub regions: RegionSubsts,
@@ -93,7 +94,7 @@ pub struct Substs {
9394
* Represents the values to use when substituting lifetime parameters.
9495
* If the value is `ErasedRegions`, then this subst is occurring during
9596
* trans, and all region parameters will be replaced with `ty::ReStatic`. */
96-
#[deriving(Clone, PartialEq, Eq, Hash)]
97+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
9798
pub enum RegionSubsts {
9899
ErasedRegions,
99100
NonerasedRegions(VecPerParamSpace<ty::Region>)
@@ -275,6 +276,17 @@ pub struct VecPerParamSpace<T> {
275276
content: Vec<T>,
276277
}
277278

279+
impl<T:fmt::Show> fmt::Show for VecPerParamSpace<T> {
280+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
281+
try!(write!(fmt, "VecPerParamSpace {{"));
282+
for space in ParamSpace::all().iter() {
283+
try!(write!(fmt, "{}: {}, ", *space, self.get_slice(*space)));
284+
}
285+
try!(write!(fmt, "}}"));
286+
Ok(())
287+
}
288+
}
289+
278290
impl<T:Clone> VecPerParamSpace<T> {
279291
pub fn push_all(&mut self, space: ParamSpace, values: &[T]) {
280292
// FIXME (#15435): slow; O(n^2); could enhance vec to make it O(n).
@@ -558,10 +570,22 @@ impl<'a> TypeFolder for SubstFolder<'a> {
558570
// the specialized routine
559571
// `middle::typeck::check::regionmanip::replace_late_regions_in_fn_sig()`.
560572
match r {
561-
ty::ReEarlyBound(_, space, i, _) => {
573+
ty::ReEarlyBound(_, space, i, region_name) => {
562574
match self.substs.regions {
563575
ErasedRegions => ty::ReStatic,
564-
NonerasedRegions(ref regions) => *regions.get(space, i),
576+
NonerasedRegions(ref regions) =>
577+
match regions.opt_get(space, i) {
578+
Some(t) => *t,
579+
None => {
580+
let span = self.span.unwrap_or(DUMMY_SP);
581+
self.tcx().sess.span_bug(
582+
span,
583+
format!("Type parameter out of range \
584+
when substituting in region {} (root type={})",
585+
region_name.as_str(),
586+
self.root_ty.repr(self.tcx())).as_slice());
587+
}
588+
}
565589
}
566590
}
567591
_ => r

src/librustc/middle/ty.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Method {
124124
}
125125
}
126126

127-
#[deriving(Clone, PartialEq, Eq, Hash)]
127+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
128128
pub struct mt {
129129
pub ty: t,
130130
pub mutbl: ast::Mutability,
@@ -138,7 +138,7 @@ pub enum TraitStore {
138138
RegionTraitStore(Region, ast::Mutability),
139139
}
140140

141-
#[deriving(Clone)]
141+
#[deriving(Clone, Show)]
142142
pub struct field_ty {
143143
pub name: Name,
144144
pub id: DefId,
@@ -394,6 +394,7 @@ pub enum tbox_flag {
394394

395395
pub type t_box = &'static t_box_;
396396

397+
#[deriving(Show)]
397398
pub struct t_box_ {
398399
pub sty: sty,
399400
pub id: uint,
@@ -436,14 +437,14 @@ pub fn type_needs_infer(t: t) -> bool {
436437
}
437438
pub fn type_id(t: t) -> uint { get(t).id }
438439

439-
#[deriving(Clone, PartialEq, Eq, Hash)]
440+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
440441
pub struct BareFnTy {
441442
pub fn_style: ast::FnStyle,
442443
pub abi: abi::Abi,
443444
pub sig: FnSig,
444445
}
445446

446-
#[deriving(Clone, PartialEq, Eq, Hash)]
447+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
447448
pub struct ClosureTy {
448449
pub fn_style: ast::FnStyle,
449450
pub onceness: ast::Onceness,
@@ -472,7 +473,7 @@ pub struct FnSig {
472473
pub variadic: bool
473474
}
474475

475-
#[deriving(Clone, PartialEq, Eq, Hash)]
476+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
476477
pub struct ParamTy {
477478
pub space: subst::ParamSpace,
478479
pub idx: uint,
@@ -712,7 +713,7 @@ mod primitives {
712713

713714
// NB: If you change this, you'll probably want to change the corresponding
714715
// AST structure in libsyntax/ast.rs as well.
715-
#[deriving(Clone, PartialEq, Eq, Hash)]
716+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
716717
pub enum sty {
717718
ty_nil,
718719
ty_bot,
@@ -741,14 +742,14 @@ pub enum sty {
741742
// on non-useful type error messages)
742743
}
743744

744-
#[deriving(Clone, PartialEq, Eq, Hash)]
745+
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
745746
pub struct TyTrait {
746747
pub def_id: DefId,
747748
pub substs: Substs,
748749
pub bounds: BuiltinBounds
749750
}
750751

751-
#[deriving(PartialEq, Eq, Hash)]
752+
#[deriving(PartialEq, Eq, Hash, Show)]
752753
pub struct TraitRef {
753754
pub def_id: DefId,
754755
pub substs: Substs,
@@ -808,7 +809,7 @@ pub enum type_err {
808809
terr_variadic_mismatch(expected_found<bool>)
809810
}
810811

811-
#[deriving(PartialEq, Eq, Hash)]
812+
#[deriving(PartialEq, Eq, Hash, Show)]
812813
pub struct ParamBounds {
813814
pub builtin_bounds: BuiltinBounds,
814815
pub trait_bounds: Vec<Rc<TraitRef>>
@@ -948,7 +949,7 @@ impl fmt::Show for IntVarValue {
948949
}
949950
}
950951

951-
#[deriving(Clone)]
952+
#[deriving(Clone, Show)]
952953
pub struct TypeParameterDef {
953954
pub ident: ast::Ident,
954955
pub def_id: ast::DefId,
@@ -958,7 +959,7 @@ pub struct TypeParameterDef {
958959
pub default: Option<ty::t>
959960
}
960961

961-
#[deriving(Encodable, Decodable, Clone)]
962+
#[deriving(Encodable, Decodable, Clone, Show)]
962963
pub struct RegionParameterDef {
963964
pub name: ast::Name,
964965
pub def_id: ast::DefId,
@@ -968,7 +969,7 @@ pub struct RegionParameterDef {
968969

969970
/// Information about the type/lifetime parameters associated with an
970971
/// item or method. Analogous to ast::Generics.
971-
#[deriving(Clone)]
972+
#[deriving(Clone, Show)]
972973
pub struct Generics {
973974
pub types: VecPerParamSpace<TypeParameterDef>,
974975
pub regions: VecPerParamSpace<RegionParameterDef>,
@@ -983,6 +984,10 @@ impl Generics {
983984
pub fn has_type_params(&self, space: subst::ParamSpace) -> bool {
984985
!self.types.is_empty_in(space)
985986
}
987+
988+
pub fn has_region_params(&self, space: subst::ParamSpace) -> bool {
989+
!self.regions.is_empty_in(space)
990+
}
986991
}
987992

988993
/// When type checking, we use the `ParameterEnvironment` to track
@@ -1014,7 +1019,7 @@ pub struct ParameterEnvironment {
10141019
/// - `generics`: the set of type parameters and their bounds
10151020
/// - `ty`: the base types, which may reference the parameters defined
10161021
/// in `generics`
1017-
#[deriving(Clone)]
1022+
#[deriving(Clone, Show)]
10181023
pub struct Polytype {
10191024
pub generics: Generics,
10201025
pub ty: t

src/librustc/middle/typeck/astconv.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn opt_ast_region_to_region<AC:AstConv,RS:RegionScope>(
141141
}
142142
};
143143

144-
debug!("opt_ast_region_to_region(opt_lifetime={:?}) yields {}",
144+
debug!("opt_ast_region_to_region(opt_lifetime={}) yields {}",
145145
opt_lifetime.as_ref().map(|e| lifetime_to_string(e)),
146146
r.repr(this.tcx()));
147147

@@ -504,6 +504,7 @@ pub fn ast_ty_to_builtin_ty<AC:AstConv,
504504
}
505505
}
506506

507+
#[deriving(Show)]
507508
enum PointerTy {
508509
Box,
509510
RPtr(ty::Region),
@@ -565,7 +566,7 @@ fn mk_pointer<AC:AstConv,
565566
constr: |ty::t| -> ty::t)
566567
-> ty::t {
567568
let tcx = this.tcx();
568-
debug!("mk_pointer(ptr_ty={:?})", ptr_ty);
569+
debug!("mk_pointer(ptr_ty={})", ptr_ty);
569570

570571
match a_seq_ty.ty.node {
571572
ast::TyVec(ref ty) => {

src/librustc/middle/typeck/check/_match.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,20 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
507507
ty::ty_struct(cid, ref substs) => {
508508
// Verify that the pattern named the right structure.
509509
let item_did = tcx.def_map.borrow().get(&pat.id).def_id();
510-
let struct_did =
511-
ty::ty_to_def_id(
512-
ty::lookup_item_type(tcx, item_did).ty).unwrap();
513-
if struct_did != cid {
514-
span_err!(tcx.sess, pat.span, E0032,
515-
"`{}` does not name the structure `{}`",
516-
pprust::path_to_string(path),
517-
fcx.infcx().ty_to_string(expected));
510+
match ty::ty_to_def_id(ty::lookup_item_type(tcx, item_did).ty) {
511+
Some(struct_did) if struct_did != cid => {
512+
span_err!(tcx.sess, path.span, E0032,
513+
"`{}` does not name the structure `{}`",
514+
pprust::path_to_string(path),
515+
fcx.infcx().ty_to_string(expected));
516+
},
517+
Some(_) => {},
518+
None => {
519+
tcx.sess.span_bug(
520+
path.span,
521+
format!("This shouldn't happen: failed to lookup structure. \
522+
item_did = {}", item_did).as_slice())
523+
},
518524
}
519525

520526
check_struct_pat(pcx, pat.id, pat.span, expected, path,

src/librustc/middle/typeck/collect.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1018,16 +1018,17 @@ fn ty_generics(ccx: &CrateCtxt,
10181018
let mut result = base_generics;
10191019

10201020
for (i, l) in lifetimes.iter().enumerate() {
1021-
result.regions.push(space,
1022-
ty::RegionParameterDef { name: l.name,
1023-
space: space,
1024-
index: i,
1025-
def_id: local_def(l.id) });
1021+
let def = ty::RegionParameterDef { name: l.name,
1022+
space: space,
1023+
index: i,
1024+
def_id: local_def(l.id) };
1025+
debug!("ty_generics: def for region param: {}", def);
1026+
result.regions.push(space, def);
10261027
}
10271028

10281029
for (i, param) in types.iter().enumerate() {
10291030
let def = get_or_create_type_parameter_def(ccx, space, param, i);
1030-
debug!("def for param: {}", def.repr(ccx.tcx));
1031+
debug!("ty_generics: def for type param: {}", def.repr(ccx.tcx));
10311032
result.types.push(space, def);
10321033
}
10331034

src/librustc/middle/typeck/infer/region_inference/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a> RegionVarBindings<'a> {
246246
if self.in_snapshot() {
247247
self.undo_log.borrow_mut().push(AddVar(vid));
248248
}
249-
debug!("created new region variable {:?} with origin {:?}",
249+
debug!("created new region variable {} with origin {}",
250250
vid, origin.repr(self.tcx));
251251
return vid;
252252
}

0 commit comments

Comments
 (0)