Skip to content

Commit 729505f

Browse files
committed
Fixing span manipulation and indentation of the suggestion introduced by #126187
According to: #128084 (comment) https://github.com/rust-lang/rust/pull/126187/files#r1634897691 I try to add a span field to `Block` which doesn't include brace '{' and '}', because I need to add a suggestion at the end of function's body, and this can help me find the right place. But this will make `Block` larger. I don't want to break the original span field in `Block`, I'm worried that it will cause a lot of code failures and I think it may be more intuitive for span to include braces.
1 parent 739b1fd commit 729505f

File tree

22 files changed

+117
-92
lines changed

22 files changed

+117
-92
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ pub struct Block {
544544
/// Distinguishes between `unsafe { ... }` and `{ ... }`.
545545
pub rules: BlockCheckMode,
546546
pub span: Span,
547+
// Only for function or method written by developers that do have a block,
548+
// but not including the blocks automatically inserted by the compiler.
549+
pub no_brace_span: Option<Span>,
547550
pub tokens: Option<LazyAttrTokenStream>,
548551
/// The following *isn't* a parse error, but will cause multiple errors in following stages.
549552
/// ```compile_fail
@@ -3501,7 +3504,7 @@ mod size_asserts {
35013504
static_assert_size!(AssocItem, 88);
35023505
static_assert_size!(AssocItemKind, 16);
35033506
static_assert_size!(Attribute, 32);
3504-
static_assert_size!(Block, 32);
3507+
static_assert_size!(Block, 48);
35053508
static_assert_size!(Expr, 72);
35063509
static_assert_size!(ExprKind, 40);
35073510
static_assert_size!(Fn, 160);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,8 @@ fn walk_mt<T: MutVisitor>(vis: &mut T, MutTy { ty, mutbl: _ }: &mut MutTy) {
10761076
}
10771077

10781078
pub fn walk_block<T: MutVisitor>(vis: &mut T, block: &mut P<Block>) {
1079-
let Block { id, stmts, rules: _, span, tokens, could_be_bare_literal: _ } = block.deref_mut();
1079+
let Block { id, stmts, rules: _, span, tokens, could_be_bare_literal: _, no_brace_span: _ } =
1080+
block.deref_mut();
10801081
vis.visit_id(id);
10811082
stmts.flat_map_in_place(|stmt| vis.flat_map_stmt(stmt));
10821083
visit_lazy_tts(vis, tokens);

compiler/rustc_ast/src/visit.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,15 @@ pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef)
943943
}
944944

945945
pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) -> V::Result {
946-
let Block { stmts, id: _, rules: _, span: _, tokens: _, could_be_bare_literal: _ } = block;
946+
let Block {
947+
stmts,
948+
id: _,
949+
rules: _,
950+
span: _,
951+
tokens: _,
952+
could_be_bare_literal: _,
953+
no_brace_span: _,
954+
} = block;
947955
walk_list!(visitor, visit_stmt, stmts);
948956
V::Result::output()
949957
}

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
22
use rustc_hir as hir;
3+
use rustc_span::Span;
34
use smallvec::SmallVec;
45

56
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
@@ -21,7 +22,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2122
let (stmts, expr) = self.lower_stmts(&b.stmts);
2223
let rules = self.lower_block_check_mode(&b.rules);
2324
let hir_id = self.lower_node_id(b.id);
24-
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
25+
let lower = |span: Span| -> Option<Span> { Some(self.lower_span(span)) };
26+
let no_brace_span = b.no_brace_span.and_then(lower);
27+
hir::Block {
28+
hir_id,
29+
stmts,
30+
expr,
31+
rules,
32+
span: self.lower_span(b.span),
33+
no_brace_span,
34+
targeted_by_break,
35+
}
2536
}
2637

2738
fn lower_stmts(

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
353353
hir_id: self.next_id(),
354354
rules: hir::BlockCheckMode::DefaultBlock,
355355
span,
356+
no_brace_span: None,
356357
targeted_by_break: false,
357358
});
358359

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
592592
hir_id: self.next_id(),
593593
rules: hir::BlockCheckMode::DefaultBlock,
594594
span,
595+
no_brace_span: None,
595596
targeted_by_break: false,
596597
});
597598
self.arena.alloc(hir::Expr {
@@ -2116,6 +2117,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21162117
hir_id,
21172118
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
21182119
span: self.lower_span(span),
2120+
no_brace_span: None,
21192121
targeted_by_break: false,
21202122
}),
21212123
None,

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ fn expand_format_args<'hir>(
612612
hir_id,
613613
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
614614
span: macsp,
615+
no_brace_span: None,
615616
targeted_by_break: false,
616617
}));
617618
let args = ctx.arena.alloc_from_iter([lit_pieces, args, format_options, unsafe_arg]);

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25712571
hir_id: self.next_id(),
25722572
rules: hir::BlockCheckMode::DefaultBlock,
25732573
span: self.lower_span(span),
2574+
no_brace_span: None,
25742575
targeted_by_break: false,
25752576
};
25762577
self.arena.alloc(blk)

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
112112
id: ast::DUMMY_NODE_ID,
113113
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
114114
span,
115+
no_brace_span: None,
115116
tokens: None,
116117
could_be_bare_literal: false,
117118
}))

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ impl<'a> ExtCtxt<'a> {
247247
id: ast::DUMMY_NODE_ID,
248248
rules: BlockCheckMode::Default,
249249
span,
250+
no_brace_span: None,
250251
tokens: None,
251252
could_be_bare_literal: false,
252253
})

0 commit comments

Comments
 (0)