Skip to content

Commit e6650c8

Browse files
committed
auto merge of #10142 : pythonesque/rust/issue-8263, r=catamorphism
This is, I think, the minimal change required. I would have included a test but as far as I can tell there is currently no way to precisely test that the span for an error underlines the correct word. I did verify it manually.
2 parents fc766ef + a716657 commit e6650c8

File tree

12 files changed

+31
-22
lines changed

12 files changed

+31
-22
lines changed

src/librustc/middle/moves.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl VisitContext {
420420
// specified and (2) have a type that
421421
// moves-by-default:
422422
let consume_with = with_fields.iter().any(|tf| {
423-
!fields.iter().any(|f| f.ident.name == tf.ident.name) &&
423+
!fields.iter().any(|f| f.ident.node.name == tf.ident.name) &&
424424
ty::type_moves_by_default(self.tcx, tf.mt.ty)
425425
});
426426

src/librustc/middle/privacy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -716,15 +716,15 @@ impl<'self> Visitor<()> for PrivacyVisitor<'self> {
716716
match ty::get(ty::expr_ty(self.tcx, expr)).sty {
717717
ty::ty_struct(id, _) => {
718718
for field in (*fields).iter() {
719-
self.check_field(expr.span, id, field.ident);
719+
self.check_field(expr.span, id, field.ident.node);
720720
}
721721
}
722722
ty::ty_enum(_, _) => {
723723
match self.tcx.def_map.get_copy(&expr.id) {
724724
ast::DefVariant(_, variant_id, _) => {
725725
for field in fields.iter() {
726726
self.check_field(expr.span, variant_id,
727-
field.ident);
727+
field.ident.node);
728728
}
729729
}
730730
_ => self.tcx.sess.span_bug(expr.span,

src/librustc/middle/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext,
508508
|discr, field_tys| {
509509
let cs = field_tys.iter().enumerate()
510510
.map(|(ix, &field_ty)| {
511-
match fs.iter().find(|f| field_ty.ident.name == f.ident.name) {
511+
match fs.iter().find(|f| field_ty.ident.name == f.ident.node.name) {
512512
Some(f) => const_expr(cx, (*f).expr),
513513
None => {
514514
match base_val {

src/librustc/middle/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn trans_rec_or_struct(bcx: @mut Block,
12111211
let numbered_fields = do fields.map |field| {
12121212
let opt_pos =
12131213
field_tys.iter().position(|field_ty|
1214-
field_ty.ident.name == field.ident.name);
1214+
field_ty.ident.name == field.ident.node.name);
12151215
match opt_pos {
12161216
Some(i) => {
12171217
need_base[i] = false;

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
20092009
}
20102010

20112011
fn check_struct_or_variant_fields(fcx: @mut FnCtxt,
2012+
struct_ty: ty::t,
20122013
span: Span,
20132014
class_id: ast::DefId,
20142015
node_id: ast::NodeId,
@@ -2030,28 +2031,30 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
20302031
for field in ast_fields.iter() {
20312032
let mut expected_field_type = ty::mk_err();
20322033

2033-
let pair = class_field_map.find(&field.ident.name).map(|x| *x);
2034+
let pair = class_field_map.find(&field.ident.node.name).map(|x| *x);
20342035
match pair {
20352036
None => {
2036-
tcx.sess.span_err(
2037-
field.span,
2038-
format!("structure has no field named `{}`",
2039-
tcx.sess.str_of(field.ident)));
2037+
fcx.type_error_message(
2038+
field.ident.span,
2039+
|actual| {
2040+
format!("structure `{}` has no field named `{}`",
2041+
actual, tcx.sess.str_of(field.ident.node))
2042+
}, struct_ty, None);
20402043
error_happened = true;
20412044
}
20422045
Some((_, true)) => {
20432046
tcx.sess.span_err(
2044-
field.span,
2047+
field.ident.span,
20452048
format!("field `{}` specified more than once",
2046-
tcx.sess.str_of(field.ident)));
2049+
tcx.sess.str_of(field.ident.node)));
20472050
error_happened = true;
20482051
}
20492052
Some((field_id, false)) => {
20502053
expected_field_type =
20512054
ty::lookup_field_type(
20522055
tcx, class_id, field_id, &substitutions);
20532056
class_field_map.insert(
2054-
field.ident.name, (field_id, true));
2057+
field.ident.node.name, (field_id, true));
20552058
fields_found += 1;
20562059
}
20572060
}
@@ -2161,6 +2164,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
21612164
// Look up and check the fields.
21622165
let class_fields = ty::lookup_struct_fields(tcx, class_id);
21632166
check_struct_or_variant_fields(fcx,
2167+
struct_type,
21642168
span,
21652169
class_id,
21662170
id,
@@ -2248,6 +2252,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
22482252
// Look up and check the enum variant fields.
22492253
let variant_fields = ty::lookup_struct_fields(tcx, variant_id);
22502254
check_struct_or_variant_fields(fcx,
2255+
enum_type,
22512256
span,
22522257
variant_id,
22532258
id,

src/libsyntax/ast.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,13 @@ pub struct Arm {
471471

472472
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
473473
pub struct Field {
474-
ident: Ident,
474+
ident: SpannedIdent,
475475
expr: @Expr,
476476
span: Span,
477477
}
478478

479+
pub type SpannedIdent = Spanned<Ident>;
480+
479481
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
480482
pub enum BlockCheckMode {
481483
DefaultBlock,

src/libsyntax/ext/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl AstBuilder for @ExtCtxt {
529529
self.expr(b.span, ast::ExprBlock(b))
530530
}
531531
fn field_imm(&self, span: Span, name: Ident, e: @ast::Expr) -> ast::Field {
532-
ast::Field { ident: name, expr: e, span: span }
532+
ast::Field { ident: respan(span, name), expr: e, span: span }
533533
}
534534
fn expr_struct(&self, span: Span, path: ast::Path, fields: ~[ast::Field]) -> @ast::Expr {
535535
self.expr(span, ast::ExprStruct(path, fields, None))

src/libsyntax/fold.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use ast::*;
1212
use ast;
13-
use codemap::{Span, Spanned};
13+
use codemap::{respan, Span, Spanned};
1414
use parse::token;
1515
use opt_vec::OptVec;
1616

@@ -551,7 +551,7 @@ fn fold_struct_field<T:ast_fold>(f: @struct_field, fld: &T) -> @struct_field {
551551

552552
fn fold_field_<T:ast_fold>(field: Field, folder: &T) -> Field {
553553
ast::Field {
554-
ident: folder.fold_ident(field.ident),
554+
ident: respan(field.ident.span, folder.fold_ident(field.ident.node)),
555555
expr: folder.fold_expr(field.expr),
556556
span: folder.new_span(field.span),
557557
}
@@ -797,7 +797,8 @@ pub fn noop_fold_expr<T:ast_fold>(e: @ast::Expr, folder: &T) -> @ast::Expr {
797797
folder.fold_expr(er))
798798
}
799799
ExprField(el, id, ref tys) => {
800-
ExprField(folder.fold_expr(el), folder.fold_ident(id),
800+
ExprField(folder.fold_expr(el),
801+
folder.fold_ident(id),
801802
tys.map(|x| folder.fold_ty(x)))
802803
}
803804
ExprIndex(callee_id, el, er) => {

src/libsyntax/parse/parser.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1549,10 +1549,11 @@ impl Parser {
15491549
pub fn parse_field(&self) -> Field {
15501550
let lo = self.span.lo;
15511551
let i = self.parse_ident();
1552+
let hi = self.last_span.hi;
15521553
self.expect(&token::COLON);
15531554
let e = self.parse_expr();
15541555
ast::Field {
1555-
ident: i,
1556+
ident: spanned(lo, hi, i),
15561557
expr: e,
15571558
span: mk_sp(lo, e.span.hi),
15581559
}

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ pub fn print_call_post(s: @ps,
11111111
pub fn print_expr(s: @ps, expr: &ast::Expr) {
11121112
fn print_field(s: @ps, field: &ast::Field) {
11131113
ibox(s, indent_unit);
1114-
print_ident(s, field.ident);
1114+
print_ident(s, field.ident.node);
11151115
word_space(s, ":");
11161116
print_expr(s, field.expr);
11171117
end(s);

src/test/compile-fail/issue-4736.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
struct NonCopyable(());
1212

1313
fn main() {
14-
let z = NonCopyable{ p: () }; //~ ERROR structure has no field named `p`
14+
let z = NonCopyable{ p: () }; //~ ERROR structure `NonCopyable` has no field named `p`
1515
}

src/test/compile-fail/struct-fields-too-many.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ struct BuildData {
1515
fn main() {
1616
let foo = BuildData {
1717
foo: 0,
18-
bar: 0 //~ ERROR structure has no field named `bar`
18+
bar: 0 //~ ERROR structure `BuildData` has no field named `bar`
1919
};
2020
}

0 commit comments

Comments
 (0)