Skip to content

Commit ed3fbba

Browse files
jroeschJared Roesch
authored and
Jared Roesch
committed
Fix error message spans
1 parent fbfbdd7 commit ed3fbba

File tree

12 files changed

+110
-77
lines changed

12 files changed

+110
-77
lines changed

src/librustc/ast_map/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub enum Node<'ast> {
119119
NodeStructCtor(&'ast StructDef),
120120

121121
NodeLifetime(&'ast Lifetime),
122+
NodeTyParam(&'ast TyParam)
122123
}
123124

124125
/// Represents an entry and its parent NodeID.
@@ -142,6 +143,7 @@ enum MapEntry<'ast> {
142143
EntryBlock(NodeId, &'ast Block),
143144
EntryStructCtor(NodeId, &'ast StructDef),
144145
EntryLifetime(NodeId, &'ast Lifetime),
146+
EntryTyParam(NodeId, &'ast TyParam),
145147

146148
/// Roots for node trees.
147149
RootCrate,
@@ -175,7 +177,8 @@ impl<'ast> MapEntry<'ast> {
175177
NodePat(n) => EntryPat(p, n),
176178
NodeBlock(n) => EntryBlock(p, n),
177179
NodeStructCtor(n) => EntryStructCtor(p, n),
178-
NodeLifetime(n) => EntryLifetime(p, n)
180+
NodeLifetime(n) => EntryLifetime(p, n),
181+
NodeTyParam(n) => EntryTyParam(p, n),
179182
}
180183
}
181184

@@ -194,6 +197,7 @@ impl<'ast> MapEntry<'ast> {
194197
EntryBlock(id, _) => id,
195198
EntryStructCtor(id, _) => id,
196199
EntryLifetime(id, _) => id,
200+
EntryTyParam(id, _) => id,
197201
_ => return None
198202
})
199203
}
@@ -213,6 +217,7 @@ impl<'ast> MapEntry<'ast> {
213217
EntryBlock(_, n) => NodeBlock(n),
214218
EntryStructCtor(_, n) => NodeStructCtor(n),
215219
EntryLifetime(_, n) => NodeLifetime(n),
220+
EntryTyParam(_, n) => NodeTyParam(n),
216221
_ => return None
217222
})
218223
}
@@ -573,6 +578,7 @@ impl<'ast> Map<'ast> {
573578
Some(NodePat(pat)) => pat.span,
574579
Some(NodeBlock(block)) => block.span,
575580
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
581+
Some(NodeTyParam(ty_param)) => ty_param.span,
576582
_ => return None,
577583
};
578584
Some(sp)
@@ -815,6 +821,14 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
815821
self.parent_node = parent_node;
816822
}
817823

824+
fn visit_generics(&mut self, generics: &'ast Generics) {
825+
for ty_param in generics.ty_params.iter() {
826+
self.insert(ty_param.id, NodeTyParam(ty_param));
827+
}
828+
829+
visit::walk_generics(self, generics);
830+
}
831+
818832
fn visit_trait_item(&mut self, ti: &'ast TraitItem) {
819833
let parent_node = self.parent_node;
820834
self.parent_node = ti.id;
@@ -1015,7 +1029,7 @@ impl<'a> NodePrinter for pprust::State<'a> {
10151029
NodePat(a) => self.print_pat(&*a),
10161030
NodeBlock(a) => self.print_block(&*a),
10171031
NodeLifetime(a) => self.print_lifetime(&*a),
1018-
1032+
NodeTyParam(_) => panic!("cannot print TyParam"),
10191033
// these cases do not carry enough information in the
10201034
// ast_map to reconstruct their full structure for pretty
10211035
// printing.
@@ -1123,6 +1137,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11231137
format!("lifetime {}{}",
11241138
pprust::lifetime_to_string(&**l), id_str)
11251139
}
1140+
Some(NodeTyParam(ref ty_param)) => {
1141+
format!("typaram {:?}{}", ty_param, id_str)
1142+
}
11261143
None => {
11271144
format!("unknown node{}", id_str)
11281145
}

src/librustc/metadata/tydecode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ fn parse_type_param_def_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
833833
assert_eq!(next(st), '|');
834834
let index = parse_u32(st);
835835
assert_eq!(next(st), '|');
836+
let default_def_id = parse_def_(st, NominalType, conv);
836837
let default = parse_opt(st, |st| parse_ty_(st, conv));
837838
let object_lifetime_default = parse_object_lifetime_default(st, conv);
838839

@@ -841,6 +842,7 @@ fn parse_type_param_def_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
841842
def_id: def_id,
842843
space: space,
843844
index: index,
845+
default_def_id: default_def_id,
844846
default: default,
845847
object_lifetime_default: object_lifetime_default,
846848
}

src/librustc/metadata/tyencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ pub fn enc_region_bounds<'a, 'tcx>(w: &mut Encoder,
409409

410410
pub fn enc_type_param_def<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
411411
v: &ty::TypeParameterDef<'tcx>) {
412-
mywrite!(w, "{}:{}|{}|{}|",
412+
mywrite!(w, "{}:{}|{}|{}|{}|",
413413
token::get_name(v.name), (cx.ds)(v.def_id),
414-
v.space.to_uint(), v.index);
414+
v.space.to_uint(), v.index, (cx.ds)(v.default_def_id));
415415
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
416416
enc_object_lifetime_default(w, cx, v.object_lifetime_default);
417417
}

src/librustc/middle/infer/mod.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use syntax::codemap;
4040
use syntax::codemap::{Span, DUMMY_SP};
4141
use util::nodemap::{FnvHashMap, NodeMap};
4242

43-
use ast_map;
4443
use self::combine::CombineFields;
4544
use self::region_inference::{RegionVarBindings, RegionSnapshot};
4645
use self::error_reporting::ErrorReporting;
@@ -658,6 +657,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
658657
/// must be attached to the variable when created, if it is created
659658
/// without a default, this will return None.
660659
///
660+
/// This code does not apply to integral or floating point variables,
661+
/// only to use declared defaults.
662+
///
661663
/// See `new_ty_var_with_default` to create a type variable with a default.
662664
/// See `type_variable::Default` for details about what a default entails.
663665
pub fn default(&self, ty: Ty<'tcx>) -> Option<type_variable::Default<'tcx>> {
@@ -1055,31 +1057,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10551057
substs: &mut Substs<'tcx>,
10561058
defs: &[ty::TypeParameterDef<'tcx>]) {
10571059

1058-
// This doesn't work ...
1059-
fn definition_span<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: ast::DefId) -> Span {
1060-
let parent = tcx.map.get_parent(def_id.node);
1061-
debug!("definition_span def_id={:?} parent={:?} node={:?} parent_node={:?}",
1062-
def_id, parent, tcx.map.find(def_id.node), tcx.map.find(parent));
1063-
match tcx.map.find(parent) {
1064-
None => DUMMY_SP,
1065-
Some(ref node) => match *node {
1066-
ast_map::NodeItem(ref item) => item.span,
1067-
ast_map::NodeForeignItem(ref item) => item.span,
1068-
ast_map::NodeTraitItem(ref item) => item.span,
1069-
ast_map::NodeImplItem(ref item) => item.span,
1070-
_ => DUMMY_SP
1071-
}
1072-
}
1073-
}
1074-
10751060
let mut vars = Vec::with_capacity(defs.len());
10761061

10771062
for def in defs.iter() {
1078-
let default = def.default.subst_spanned(self.tcx, substs, Some(span)).map(|default| {
1063+
let default = def.default.map(|default| {
1064+
let definition_span = self.tcx.map.opt_span(def.def_id.node);
10791065
type_variable::Default {
1080-
ty: default,
1066+
ty: default.subst_spanned(self.tcx, substs, Some(span)),
10811067
origin_span: span,
1082-
definition_span: definition_span(self.tcx, def.def_id)
1068+
definition_span: definition_span.unwrap_or(DUMMY_SP)
10831069
}
10841070
});
10851071

src/librustc/middle/ty.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ pub struct Field<'tcx> {
115115
pub mt: TypeAndMut<'tcx>
116116
}
117117

118-
119-
120118
// Enum information
121119
#[derive(Clone)]
122120
pub struct VariantInfo<'tcx> {
@@ -2282,6 +2280,7 @@ pub struct TypeParameterDef<'tcx> {
22822280
pub def_id: ast::DefId,
22832281
pub space: subst::ParamSpace,
22842282
pub index: u32,
2283+
pub default_def_id: DefId, // for use in error reporing about defaults
22852284
pub default: Option<Ty<'tcx>>,
22862285
pub object_lifetime_default: ObjectLifetimeDefault,
22872286
}
@@ -5084,7 +5083,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
50845083
values.found)
50855084
},
50865085
TyParamDefaultMismatch(ref values) => {
5087-
write!(f, "conflicting type parameter defaults {} and {}",
5086+
write!(f, "conflicting type parameter defaults `{}` and `{}`",
50885087
values.expected.ty,
50895088
values.found.ty)
50905089
}
@@ -5453,11 +5452,11 @@ impl<'tcx> ctxt<'tcx> {
54535452
expected.ty,
54545453
found.ty));
54555454
self.sess.span_note(expected.definition_span,
5456-
&format!("...a default was defined"));
5455+
&format!("a default was defined here..."));
54575456
self.sess.span_note(expected.origin_span,
54585457
&format!("...that was applied to an unconstrained type variable here"));
54595458
self.sess.span_note(found.definition_span,
5460-
&format!("...a second default was defined"));
5459+
&format!("a second default was defined here..."));
54615460
self.sess.span_note(found.origin_span,
54625461
&format!("...that also applies to the same type variable here"));
54635462
}

src/librustc/middle/ty_fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
340340
space: self.space,
341341
index: self.index,
342342
default: self.default.fold_with(folder),
343+
default_def_id: self.default_def_id,
343344
object_lifetime_default: self.object_lifetime_default.fold_with(folder),
344345
}
345346
}

src/librustc_trans/trans/monomorphize.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
266266
// Ugh -- but this ensures any new variants won't be forgotten
267267
ast_map::NodeForeignItem(..) |
268268
ast_map::NodeLifetime(..) |
269+
ast_map::NodeTyParam(..) |
269270
ast_map::NodeExpr(..) |
270271
ast_map::NodeStmt(..) |
271272
ast_map::NodeArg(..) |

src/librustc_typeck/astconv.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use middle::def;
5555
use middle::implicator::object_region_bounds;
5656
use middle::resolve_lifetime as rl;
5757
use middle::privacy::{AllPublic, LastMod};
58-
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs};
58+
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs, ParamSpace};
5959
use middle::traits;
6060
use middle::ty::{self, RegionEscape, Ty, ToPredicate, HasTypeFlags};
6161
use middle::ty_fold;
@@ -111,7 +111,11 @@ pub trait AstConv<'tcx> {
111111
}
112112

113113
/// What type should we use when a type is omitted?
114-
fn ty_infer(&self, default: Option<ty::TypeParameterDef<'tcx>>, span: Span) -> Ty<'tcx>;
114+
fn ty_infer(&self,
115+
param_and_substs: Option<ty::TypeParameterDef<'tcx>>,
116+
substs: Option<&mut Substs<'tcx>>,
117+
space: Option<ParamSpace>,
118+
span: Span) -> Ty<'tcx>;
115119

116120
/// Projecting an associated type from a (potentially)
117121
/// higher-ranked trait reference is more complicated, because of
@@ -403,7 +407,11 @@ fn create_substs_for_ast_path<'tcx>(
403407
// they were optional (e.g. paths inside expressions).
404408
let mut type_substs = if param_mode == PathParamMode::Optional &&
405409
types_provided.is_empty() {
406-
ty_param_defs.iter().map(|p| this.ty_infer(Some(p.clone()), span)).collect()
410+
let mut substs = region_substs.clone();
411+
ty_param_defs
412+
.iter()
413+
.map(|p| this.ty_infer(Some(p.clone()), Some(&mut substs), Some(TypeSpace), span))
414+
.collect()
407415
} else {
408416
types_provided
409417
};
@@ -1661,7 +1669,7 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
16611669
// values in a ExprClosure, or as
16621670
// the type of local variables. Both of these cases are
16631671
// handled specially and will not descend into this routine.
1664-
this.ty_infer(None, ast_ty.span)
1672+
this.ty_infer(None, None, None, ast_ty.span)
16651673
}
16661674
};
16671675

@@ -1677,7 +1685,7 @@ pub fn ty_of_arg<'tcx>(this: &AstConv<'tcx>,
16771685
{
16781686
match a.ty.node {
16791687
ast::TyInfer if expected_ty.is_some() => expected_ty.unwrap(),
1680-
ast::TyInfer => this.ty_infer(None, a.ty.span),
1688+
ast::TyInfer => this.ty_infer(None, None, None, a.ty.span),
16811689
_ => ast_ty_to_ty(this, rscope, &*a.ty),
16821690
}
16831691
}
@@ -1796,7 +1804,7 @@ fn ty_of_method_or_bare_fn<'a, 'tcx>(this: &AstConv<'tcx>,
17961804

17971805
let output_ty = match decl.output {
17981806
ast::Return(ref output) if output.node == ast::TyInfer =>
1799-
ty::FnConverging(this.ty_infer(None, output.span)),
1807+
ty::FnConverging(this.ty_infer(None, None, None, output.span)),
18001808
ast::Return(ref output) =>
18011809
ty::FnConverging(convert_ty_with_lifetime_elision(this,
18021810
implied_output_region,
@@ -1936,7 +1944,7 @@ pub fn ty_of_closure<'tcx>(
19361944
_ if is_infer && expected_ret_ty.is_some() =>
19371945
expected_ret_ty.unwrap(),
19381946
_ if is_infer =>
1939-
ty::FnConverging(this.ty_infer(None, decl.output.span())),
1947+
ty::FnConverging(this.ty_infer(None, None, None, decl.output.span())),
19401948
ast::Return(ref output) =>
19411949
ty::FnConverging(ast_ty_to_ty(this, &rb, &**output)),
19421950
ast::DefaultReturn(..) => unreachable!(),

0 commit comments

Comments
 (0)