@@ -306,6 +306,9 @@ pub(crate) fn highlight_exit_points(
306
306
_ => None ,
307
307
} ;
308
308
309
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
310
+ highlights. push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
311
+ }
309
312
} ) ;
310
313
311
314
// We should handle `return` separately because when it is used in `try` block
@@ -322,6 +325,10 @@ pub(crate) fn highlight_exit_points(
322
325
_ => None ,
323
326
} ;
324
327
328
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
329
+ highlights
330
+ . push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
331
+ }
325
332
} ) ;
326
333
327
334
let tail = match body {
@@ -411,6 +418,10 @@ pub(crate) fn highlight_break_points(
411
418
token_lt. map ( |it| it. syntax ( ) . text_range ( ) ) ,
412
419
) ;
413
420
421
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
422
+ highlights
423
+ . push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
424
+ }
414
425
} ) ;
415
426
416
427
Some ( highlights)
@@ -483,6 +494,9 @@ pub(crate) fn highlight_yield_points(
483
494
}
484
495
. map ( |it| it. text_range ( ) ) ;
485
496
497
+ if let Some ( range) = original_range ( sema. db , file_id, token_range) {
498
+ highlights. push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } ) ;
499
+ }
486
500
} ) ;
487
501
488
502
Some ( highlights)
@@ -522,6 +536,20 @@ fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSe
522
536
. collect ( )
523
537
}
524
538
539
+ fn original_range (
540
+ db : & dyn db:: ExpandDatabase ,
541
+ file_id : HirFileId ,
542
+ text_range : Option < TextRange > ,
543
+ ) -> Option < TextRange > {
544
+ if text_range. is_none ( ) || !file_id. is_macro ( ) {
545
+ return text_range;
546
+ }
547
+
548
+ InFile :: new ( file_id, text_range. unwrap ( ) )
549
+ . original_node_file_range_opt ( db)
550
+ . map ( |( frange, _) | frange. range )
551
+ }
552
+
525
553
/// Preorder walk all the expression's child expressions.
526
554
/// For macro calls, the callback will be called on the expanded expressions after
527
555
/// visiting the macro call itself.
@@ -989,6 +1017,7 @@ impl Never {
989
1017
}
990
1018
macro_rules! never {
991
1019
() => { never() }
1020
+ // ^^^^^^^
992
1021
}
993
1022
fn never() -> ! { loop {} }
994
1023
fn foo() ->$0 u32 {
@@ -1815,4 +1844,108 @@ fn test() {
1815
1844
"# ,
1816
1845
) ;
1817
1846
}
1847
+
1848
+ #[ test]
1849
+ fn return_in_macros ( ) {
1850
+ check (
1851
+ r#"
1852
+ macro_rules! N {
1853
+ ($i:ident, $x:expr, $blk:expr) => {
1854
+ for $i in 0..$x {
1855
+ $blk
1856
+ }
1857
+ };
1858
+ }
1859
+
1860
+ fn main() {
1861
+ fn f() {
1862
+ // ^^
1863
+ N!(i, 5, {
1864
+ println!("{}", i);
1865
+ return$0;
1866
+ // ^^^^^^
1867
+ });
1868
+
1869
+ for i in 1..5 {
1870
+ return;
1871
+ // ^^^^^^
1872
+ }
1873
+ (|| {
1874
+ return;
1875
+ })();
1876
+ }
1877
+ }
1878
+ "# ,
1879
+ )
1880
+ }
1881
+
1882
+ #[ test]
1883
+ fn return_in_closure ( ) {
1884
+ check (
1885
+ r#"
1886
+ macro_rules! N {
1887
+ ($i:ident, $x:expr, $blk:expr) => {
1888
+ for $i in 0..$x {
1889
+ $blk
1890
+ }
1891
+ };
1892
+ }
1893
+
1894
+ fn main() {
1895
+ fn f() {
1896
+ N!(i, 5, {
1897
+ println!("{}", i);
1898
+ return;
1899
+ });
1900
+
1901
+ for i in 1..5 {
1902
+ return;
1903
+ }
1904
+ (|| {
1905
+ // ^
1906
+ return$0;
1907
+ // ^^^^^^
1908
+ })();
1909
+ }
1910
+ }
1911
+ "# ,
1912
+ )
1913
+ }
1914
+
1915
+ #[ test]
1916
+ fn return_in_try ( ) {
1917
+ check (
1918
+ r#"
1919
+ fn main() {
1920
+ fn f() {
1921
+ // ^^
1922
+ try {
1923
+ return$0;
1924
+ // ^^^^^^
1925
+ }
1926
+
1927
+ return;
1928
+ // ^^^^^^
1929
+ }
1930
+ }
1931
+ "# ,
1932
+ )
1933
+ }
1934
+
1935
+ #[ test]
1936
+ fn break_in_try ( ) {
1937
+ check (
1938
+ r#"
1939
+ fn main() {
1940
+ for i in 1..100 {
1941
+ // ^^^
1942
+ let x: Result<(), ()> = try {
1943
+ break$0;
1944
+ // ^^^^^
1945
+ };
1946
+ }
1947
+ }
1948
+ "# ,
1949
+ )
1950
+ }
1818
1951
}
0 commit comments