Skip to content

Commit 99e4127

Browse files
authored
Rollup merge of #114434 - Nilstrieb:indexing-spans, r=est31
Improve spans for indexing expressions fixes #114388 Indexing is similar to method calls in having an arbitrary left-hand-side and then something on the right, which is the main part of the expression. Method calls already have a span for that right part, but indexing does not. This means that long method chains that use indexing have really bad spans, especially when the indexing panics and that span in coverted into a panic location. This does the same thing as method calls for the AST and HIR, storing an extra span which is then put into the `fn_span` field in THIR. r? compiler-errors
2 parents 4669905 + 5706be1 commit 99e4127

File tree

82 files changed

+192
-149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+192
-149
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,8 @@ pub enum ExprKind {
14621462
/// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct field.
14631463
Field(P<Expr>, Ident),
14641464
/// An indexing operation (e.g., `foo[2]`).
1465-
Index(P<Expr>, P<Expr>),
1465+
/// The span represents the span of the `[2]`, including brackets.
1466+
Index(P<Expr>, P<Expr>, Span),
14661467
/// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`; and `..` in destructuring assignment).
14671468
Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
14681469
/// An underscore, used in destructuring assignment to ignore a value.

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,14 +1400,15 @@ pub fn noop_visit_expr<T: MutVisitor>(
14001400
fn_decl,
14011401
body,
14021402
fn_decl_span,
1403-
fn_arg_span: _,
1403+
fn_arg_span,
14041404
}) => {
14051405
vis.visit_closure_binder(binder);
14061406
visit_constness(constness, vis);
14071407
vis.visit_asyncness(asyncness);
14081408
vis.visit_fn_decl(fn_decl);
14091409
vis.visit_expr(body);
14101410
vis.visit_span(fn_decl_span);
1411+
vis.visit_span(fn_arg_span);
14111412
}
14121413
ExprKind::Block(blk, label) => {
14131414
vis.visit_block(blk);
@@ -1420,9 +1421,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
14201421
vis.visit_expr(expr);
14211422
vis.visit_span(await_kw_span);
14221423
}
1423-
ExprKind::Assign(el, er, _) => {
1424+
ExprKind::Assign(el, er, span) => {
14241425
vis.visit_expr(el);
14251426
vis.visit_expr(er);
1427+
vis.visit_span(span);
14261428
}
14271429
ExprKind::AssignOp(_op, el, er) => {
14281430
vis.visit_expr(el);
@@ -1432,9 +1434,10 @@ pub fn noop_visit_expr<T: MutVisitor>(
14321434
vis.visit_expr(el);
14331435
vis.visit_ident(ident);
14341436
}
1435-
ExprKind::Index(el, er) => {
1437+
ExprKind::Index(el, er, brackets_span) => {
14361438
vis.visit_expr(el);
14371439
vis.visit_expr(er);
1440+
vis.visit_span(brackets_span);
14381441
}
14391442
ExprKind::Range(e1, e2, _lim) => {
14401443
visit_opt(e1, |e1| vis.visit_expr(e1));

compiler/rustc_ast/src/util/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
390390
| ast::ExprKind::Cast(x, _)
391391
| ast::ExprKind::Type(x, _)
392392
| ast::ExprKind::Field(x, _)
393-
| ast::ExprKind::Index(x, _) => {
393+
| ast::ExprKind::Index(x, _, _) => {
394394
// &X { y: 1 }, X { y: 1 }.y
395395
contains_exterior_struct_lit(x)
396396
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
885885
visitor.visit_expr(subexpression);
886886
visitor.visit_ident(*ident);
887887
}
888-
ExprKind::Index(main_expression, index_expression) => {
888+
ExprKind::Index(main_expression, index_expression, _) => {
889889
visitor.visit_expr(main_expression);
890890
visitor.visit_expr(index_expression)
891891
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
240240
ExprKind::Field(el, ident) => {
241241
hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
242242
}
243-
ExprKind::Index(el, er) => {
244-
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er))
243+
ExprKind::Index(el, er, brackets_span) => {
244+
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
245245
}
246246
ExprKind::Range(Some(e1), Some(e2), RangeLimits::Closed) => {
247247
self.lower_expr_range_closed(e.span, e1, e2)

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'a> State<'a> {
477477
self.word(".");
478478
self.print_ident(*ident);
479479
}
480-
ast::ExprKind::Index(expr, index) => {
480+
ast::ExprKind::Index(expr, index, _) => {
481481
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
482482
self.word("[");
483483
self.print_expr(index);

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
7979
| hir::ExprKind::Unary(hir::UnOp::Deref, inner)
8080
| hir::ExprKind::Field(inner, _)
8181
| hir::ExprKind::MethodCall(_, inner, _, _)
82-
| hir::ExprKind::Index(inner, _) = &expr.kind
82+
| hir::ExprKind::Index(inner, _, _) = &expr.kind
8383
{
8484
expr = inner;
8585
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
567567
}
568568
};
569569
if let hir::ExprKind::Assign(place, rv, _sp) = expr.kind
570-
&& let hir::ExprKind::Index(val, index) = place.kind
570+
&& let hir::ExprKind::Index(val, index, _) = place.kind
571571
&& (expr.span == self.assign_span || place.span == self.assign_span)
572572
{
573573
// val[index] = rv;
@@ -620,7 +620,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
620620
);
621621
self.suggested = true;
622622
} else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
623-
&& let hir::ExprKind::Index(val, index) = receiver.kind
623+
&& let hir::ExprKind::Index(val, index, _) = receiver.kind
624624
&& expr.span == self.assign_span
625625
{
626626
// val[index].path(args..);

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
237237
ExprKind::If(local_expr, _, _) => {
238238
self.manage_cond_expr(local_expr);
239239
}
240-
ExprKind::Index(prefix, suffix) => {
240+
ExprKind::Index(prefix, suffix, _) => {
241241
self.manage_cond_expr(prefix);
242242
self.manage_cond_expr(suffix);
243243
}

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ impl Expr<'_> {
17541754

17551755
ExprKind::Unary(UnOp::Deref, _) => true,
17561756

1757-
ExprKind::Field(ref base, _) | ExprKind::Index(ref base, _) => {
1757+
ExprKind::Field(ref base, _) | ExprKind::Index(ref base, _, _) => {
17581758
allow_projections_from(base) || base.is_place_expr(allow_projections_from)
17591759
}
17601760

@@ -1831,7 +1831,7 @@ impl Expr<'_> {
18311831
ExprKind::Type(base, _)
18321832
| ExprKind::Unary(_, base)
18331833
| ExprKind::Field(base, _)
1834-
| ExprKind::Index(base, _)
1834+
| ExprKind::Index(base, _, _)
18351835
| ExprKind::AddrOf(.., base)
18361836
| ExprKind::Cast(base, _) => {
18371837
// This isn't exactly true for `Index` and all `Unary`, but we are using this
@@ -2015,7 +2015,9 @@ pub enum ExprKind<'hir> {
20152015
/// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct or tuple field.
20162016
Field(&'hir Expr<'hir>, Ident),
20172017
/// An indexing operation (`foo[2]`).
2018-
Index(&'hir Expr<'hir>, &'hir Expr<'hir>),
2018+
/// Similar to [`ExprKind::MethodCall`], the final `Span` represents the span of the brackets
2019+
/// and index.
2020+
Index(&'hir Expr<'hir>, &'hir Expr<'hir>, Span),
20192021

20202022
/// Path to a definition, possibly containing lifetime or type parameters.
20212023
Path(QPath<'hir>),

0 commit comments

Comments
 (0)