Skip to content

Commit 54c61eb

Browse files
authored
Merge pull request rust-lang#19589 from roife/fix-issue-19138
fix: highlighting for tail expr in labelled blocks
2 parents c7f35ad + 9333cc7 commit 54c61eb

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/tools/rust-analyzer/crates/ide/src/highlight_related.rs

+66
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ fn highlight_references(
232232
}
233233
}
234234

235+
// highlight the tail expr of the labelled block
236+
if matches!(def, Definition::Label(_)) {
237+
let label = token.parent_ancestors().nth(1).and_then(ast::Label::cast);
238+
if let Some(block) =
239+
label.and_then(|label| label.syntax().parent()).and_then(ast::BlockExpr::cast)
240+
{
241+
for_each_tail_expr(&block.into(), &mut |tail| {
242+
if !matches!(tail, ast::Expr::BreakExpr(_)) {
243+
res.insert(HighlightedRange {
244+
range: tail.syntax().text_range(),
245+
category: ReferenceCategory::empty(),
246+
});
247+
}
248+
});
249+
}
250+
}
251+
235252
// highlight the defs themselves
236253
match def {
237254
Definition::Local(local) => {
@@ -446,6 +463,18 @@ pub(crate) fn highlight_break_points(
446463
push_to_highlights(file_id, text_range);
447464
});
448465

466+
if matches!(expr, ast::Expr::BlockExpr(_)) {
467+
for_each_tail_expr(&expr, &mut |tail| {
468+
if matches!(tail, ast::Expr::BreakExpr(_)) {
469+
return;
470+
}
471+
472+
let file_id = sema.hir_file_for(tail.syntax());
473+
let range = tail.syntax().text_range();
474+
push_to_highlights(file_id, Some(range));
475+
});
476+
}
477+
449478
Some(highlights)
450479
}
451480

@@ -2072,4 +2101,41 @@ pub unsafe fn bootstrap() -> ! {
20722101
"#,
20732102
)
20742103
}
2104+
2105+
#[test]
2106+
fn labeled_block_tail_expr() {
2107+
check(
2108+
r#"
2109+
fn foo() {
2110+
'a: {
2111+
// ^^^
2112+
if true { break$0 'a 0; }
2113+
// ^^^^^^^^
2114+
5
2115+
// ^
2116+
}
2117+
}
2118+
"#,
2119+
);
2120+
}
2121+
2122+
#[test]
2123+
fn labeled_block_tail_expr_2() {
2124+
check(
2125+
r#"
2126+
fn foo() {
2127+
let _ = 'b$0lk: {
2128+
// ^^^^
2129+
let x = 1;
2130+
if true { break 'blk 42; }
2131+
// ^^^^
2132+
if false { break 'blk 24; }
2133+
// ^^^^
2134+
100
2135+
// ^^^
2136+
};
2137+
}
2138+
"#,
2139+
);
2140+
}
20752141
}

0 commit comments

Comments
 (0)