Skip to content

Commit 815f26c

Browse files
committed
Fix error message spans
1 parent 3ed227d commit 815f26c

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
}
@@ -567,6 +572,7 @@ impl<'ast> Map<'ast> {
567572
Some(NodePat(pat)) => pat.span,
568573
Some(NodeBlock(block)) => block.span,
569574
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
575+
Some(NodeTyParam(ty_param)) => ty_param.span,
570576
_ => return None,
571577
};
572578
Some(sp)
@@ -809,6 +815,14 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
809815
self.parent_node = parent_node;
810816
}
811817

818+
fn visit_generics(&mut self, generics: &'ast Generics) {
819+
for ty_param in generics.ty_params.iter() {
820+
self.insert(ty_param.id, NodeTyParam(ty_param));
821+
}
822+
823+
visit::walk_generics(self, generics);
824+
}
825+
812826
fn visit_trait_item(&mut self, ti: &'ast TraitItem) {
813827
let parent_node = self.parent_node;
814828
self.parent_node = ti.id;
@@ -1009,7 +1023,7 @@ impl<'a> NodePrinter for pprust::State<'a> {
10091023
NodePat(a) => self.print_pat(&*a),
10101024
NodeBlock(a) => self.print_block(&*a),
10111025
NodeLifetime(a) => self.print_lifetime(&*a),
1012-
1026+
NodeTyParam(_) => panic!("cannot print TyParam"),
10131027
// these cases do not carry enough information in the
10141028
// ast_map to reconstruct their full structure for pretty
10151029
// printing.
@@ -1117,6 +1131,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11171131
format!("lifetime {}{}",
11181132
pprust::lifetime_to_string(&**l), id_str)
11191133
}
1134+
Some(NodeTyParam(ref ty_param)) => {
1135+
format!("typaram {:?}{}", ty_param, id_str)
1136+
}
11201137
None => {
11211138
format!("unknown node{}", id_str)
11221139
}

src/librustc/metadata/tydecode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ fn parse_type_param_def_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
828828
assert_eq!(next(st), '|');
829829
let index = parse_u32(st);
830830
assert_eq!(next(st), '|');
831+
let default_def_id = parse_def_(st, NominalType, conv);
831832
let default = parse_opt(st, |st| parse_ty_(st, conv));
832833
let object_lifetime_default = parse_object_lifetime_default(st, conv);
833834

@@ -836,6 +837,7 @@ fn parse_type_param_def_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
836837
def_id: def_id,
837838
space: space,
838839
index: index,
840+
default_def_id: default_def_id,
839841
default: default,
840842
object_lifetime_default: object_lifetime_default,
841843
}

src/librustc/metadata/tyencode.rs

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

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

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;
@@ -661,6 +660,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
661660
/// must be attached to the variable when created, if it is created
662661
/// without a default, this will return None.
663662
///
663+
/// This code does not apply to integral or floating point variables,
664+
/// only to use declared defaults.
665+
///
664666
/// See `new_ty_var_with_default` to create a type variable with a default.
665667
/// See `type_variable::Default` for details about what a default entails.
666668
pub fn default(&self, ty: Ty<'tcx>) -> Option<type_variable::Default<'tcx>> {
@@ -1056,31 +1058,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10561058
substs: &mut Substs<'tcx>,
10571059
defs: &[ty::TypeParameterDef<'tcx>]) {
10581060

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

10781063
for def in defs.iter() {
1079-
let default = def.default.subst_spanned(self.tcx, substs, Some(span)).map(|default| {
1064+
let default = def.default.map(|default| {
1065+
let definition_span = self.tcx.map.opt_span(def.def_id.node);
10801066
type_variable::Default {
1081-
ty: default,
1067+
ty: default.subst_spanned(self.tcx, substs, Some(span)),
10821068
origin_span: span,
1083-
definition_span: definition_span(self.tcx, def.def_id)
1069+
definition_span: definition_span.unwrap_or(DUMMY_SP)
10841070
}
10851071
});
10861072

src/librustc/middle/ty.rs

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

117-
118-
119117
// Enum information
120118
#[derive(Clone)]
121119
pub struct VariantInfo<'tcx> {
@@ -2199,6 +2197,7 @@ pub struct TypeParameterDef<'tcx> {
21992197
pub def_id: ast::DefId,
22002198
pub space: subst::ParamSpace,
22012199
pub index: u32,
2200+
pub default_def_id: DefId, // for use in error reporing about defaults
22022201
pub default: Option<Ty<'tcx>>,
22032202
pub object_lifetime_default: ObjectLifetimeDefault,
22042203
}
@@ -5102,7 +5101,7 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
51025101
values.found)
51035102
},
51045103
TyParamDefaultMismatch(ref values) => {
5105-
write!(f, "conflicting type parameter defaults {} and {}",
5104+
write!(f, "conflicting type parameter defaults `{}` and `{}`",
51065105
values.expected.ty,
51075106
values.found.ty)
51085107
}
@@ -5471,11 +5470,11 @@ impl<'tcx> ctxt<'tcx> {
54715470
expected.ty,
54725471
found.ty));
54735472
self.sess.span_note(expected.definition_span,
5474-
&format!("...a default was defined"));
5473+
&format!("a default was defined here..."));
54755474
self.sess.span_note(expected.origin_span,
54765475
&format!("...that was applied to an unconstrained type variable here"));
54775476
self.sess.span_note(found.definition_span,
5478-
&format!("...a second default was defined"));
5477+
&format!("a second default was defined here..."));
54795478
self.sess.span_note(found.origin_span,
54805479
&format!("...that also applies to the same type variable here"));
54815480
}

src/librustc/middle/ty_fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
330330
space: self.space,
331331
index: self.index,
332332
default: self.default.fold_with(folder),
333+
default_def_id: self.default_def_id,
333334
object_lifetime_default: self.object_lifetime_default.fold_with(folder),
334335
}
335336
}

src/librustc_trans/trans/monomorphize.rs

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

src/librustc_typeck/astconv.rs

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

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

114118
/// Projecting an associated type from a (potentially)
115119
/// higher-ranked trait reference is more complicated, because of
@@ -401,7 +405,11 @@ fn create_substs_for_ast_path<'tcx>(
401405
// they were optional (e.g. paths inside expressions).
402406
let mut type_substs = if param_mode == PathParamMode::Optional &&
403407
types_provided.is_empty() {
404-
ty_param_defs.iter().map(|p| this.ty_infer(Some(p.clone()), span)).collect()
408+
let mut substs = region_substs.clone();
409+
ty_param_defs
410+
.iter()
411+
.map(|p| this.ty_infer(Some(p.clone()), Some(&mut substs), Some(TypeSpace), span))
412+
.collect()
405413
} else {
406414
types_provided
407415
};
@@ -1665,7 +1673,7 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
16651673
// values in a ExprClosure, or as
16661674
// the type of local variables. Both of these cases are
16671675
// handled specially and will not descend into this routine.
1668-
this.ty_infer(None, ast_ty.span)
1676+
this.ty_infer(None, None, None, ast_ty.span)
16691677
}
16701678
};
16711679

@@ -1681,7 +1689,7 @@ pub fn ty_of_arg<'tcx>(this: &AstConv<'tcx>,
16811689
{
16821690
match a.ty.node {
16831691
ast::TyInfer if expected_ty.is_some() => expected_ty.unwrap(),
1684-
ast::TyInfer => this.ty_infer(None, a.ty.span),
1692+
ast::TyInfer => this.ty_infer(None, None, None, a.ty.span),
16851693
_ => ast_ty_to_ty(this, rscope, &*a.ty),
16861694
}
16871695
}
@@ -1800,7 +1808,7 @@ fn ty_of_method_or_bare_fn<'a, 'tcx>(this: &AstConv<'tcx>,
18001808

18011809
let output_ty = match decl.output {
18021810
ast::Return(ref output) if output.node == ast::TyInfer =>
1803-
ty::FnConverging(this.ty_infer(None, output.span)),
1811+
ty::FnConverging(this.ty_infer(None, None, None, output.span)),
18041812
ast::Return(ref output) =>
18051813
ty::FnConverging(convert_ty_with_lifetime_elision(this,
18061814
implied_output_region,
@@ -1940,7 +1948,7 @@ pub fn ty_of_closure<'tcx>(
19401948
_ if is_infer && expected_ret_ty.is_some() =>
19411949
expected_ret_ty.unwrap(),
19421950
_ if is_infer =>
1943-
ty::FnConverging(this.ty_infer(None, decl.output.span())),
1951+
ty::FnConverging(this.ty_infer(None, None, None, decl.output.span())),
19441952
ast::Return(ref output) =>
19451953
ty::FnConverging(ast_ty_to_ty(this, &rb, &**output)),
19461954
ast::DefaultReturn(..) => unreachable!(),

0 commit comments

Comments
 (0)