@@ -301,6 +301,9 @@ pub(crate) fn highlight_exit_points(
301
301
_ => None ,
302
302
} ;
303
303
304
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
305
+ highlights. push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
306
+ }
304
307
} ) ;
305
308
306
309
// We should handle `return` separately because when it is used in `try` block
@@ -317,6 +320,10 @@ pub(crate) fn highlight_exit_points(
317
320
_ => None ,
318
321
} ;
319
322
323
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
324
+ highlights
325
+ . push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
326
+ }
320
327
} ) ;
321
328
322
329
let tail = match body {
@@ -406,6 +413,10 @@ pub(crate) fn highlight_break_points(
406
413
token_lt. map ( |it| it. syntax ( ) . text_range ( ) ) ,
407
414
) ;
408
415
416
+ if let Some ( range) = original_range ( sema. db , file_id, text_range) {
417
+ highlights
418
+ . push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } )
419
+ }
409
420
} ) ;
410
421
411
422
Some ( highlights)
@@ -478,6 +489,9 @@ pub(crate) fn highlight_yield_points(
478
489
}
479
490
. map ( |it| it. text_range ( ) ) ;
480
491
492
+ if let Some ( range) = original_range ( sema. db , file_id, token_range) {
493
+ highlights. push ( HighlightedRange { category : ReferenceCategory :: empty ( ) , range } ) ;
494
+ }
481
495
} ) ;
482
496
483
497
Some ( highlights)
@@ -517,6 +531,20 @@ fn find_defs(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> FxHashSe
517
531
. collect ( )
518
532
}
519
533
534
+ fn original_range (
535
+ db : & dyn db:: ExpandDatabase ,
536
+ file_id : HirFileId ,
537
+ text_range : Option < TextRange > ,
538
+ ) -> Option < TextRange > {
539
+ if text_range. is_none ( ) || !file_id. is_macro ( ) {
540
+ return text_range;
541
+ }
542
+
543
+ InFile :: new ( file_id, text_range. unwrap ( ) )
544
+ . original_node_file_range_opt ( db)
545
+ . map ( |( frange, _) | frange. range )
546
+ }
547
+
520
548
/// Preorder walk all the expression's child expressions.
521
549
/// For macro calls, the callback will be called on the expanded expressions after
522
550
/// visiting the macro call itself.
@@ -984,6 +1012,7 @@ impl Never {
984
1012
}
985
1013
macro_rules! never {
986
1014
() => { never() }
1015
+ // ^^^^^^^
987
1016
}
988
1017
fn never() -> ! { loop {} }
989
1018
fn foo() ->$0 u32 {
@@ -1810,4 +1839,108 @@ fn test() {
1810
1839
"# ,
1811
1840
) ;
1812
1841
}
1842
+
1843
+ #[ test]
1844
+ fn return_in_macros ( ) {
1845
+ check (
1846
+ r#"
1847
+ macro_rules! N {
1848
+ ($i:ident, $x:expr, $blk:expr) => {
1849
+ for $i in 0..$x {
1850
+ $blk
1851
+ }
1852
+ };
1853
+ }
1854
+
1855
+ fn main() {
1856
+ fn f() {
1857
+ // ^^
1858
+ N!(i, 5, {
1859
+ println!("{}", i);
1860
+ return$0;
1861
+ // ^^^^^^
1862
+ });
1863
+
1864
+ for i in 1..5 {
1865
+ return;
1866
+ // ^^^^^^
1867
+ }
1868
+ (|| {
1869
+ return;
1870
+ })();
1871
+ }
1872
+ }
1873
+ "# ,
1874
+ )
1875
+ }
1876
+
1877
+ #[ test]
1878
+ fn return_in_closure ( ) {
1879
+ check (
1880
+ r#"
1881
+ macro_rules! N {
1882
+ ($i:ident, $x:expr, $blk:expr) => {
1883
+ for $i in 0..$x {
1884
+ $blk
1885
+ }
1886
+ };
1887
+ }
1888
+
1889
+ fn main() {
1890
+ fn f() {
1891
+ N!(i, 5, {
1892
+ println!("{}", i);
1893
+ return;
1894
+ });
1895
+
1896
+ for i in 1..5 {
1897
+ return;
1898
+ }
1899
+ (|| {
1900
+ // ^
1901
+ return$0;
1902
+ // ^^^^^^
1903
+ })();
1904
+ }
1905
+ }
1906
+ "# ,
1907
+ )
1908
+ }
1909
+
1910
+ #[ test]
1911
+ fn return_in_try ( ) {
1912
+ check (
1913
+ r#"
1914
+ fn main() {
1915
+ fn f() {
1916
+ // ^^
1917
+ try {
1918
+ return$0;
1919
+ // ^^^^^^
1920
+ }
1921
+
1922
+ return;
1923
+ // ^^^^^^
1924
+ }
1925
+ }
1926
+ "# ,
1927
+ )
1928
+ }
1929
+
1930
+ #[ test]
1931
+ fn break_in_try ( ) {
1932
+ check (
1933
+ r#"
1934
+ fn main() {
1935
+ for i in 1..100 {
1936
+ // ^^^
1937
+ let x: Result<(), ()> = try {
1938
+ break$0;
1939
+ // ^^^^^
1940
+ };
1941
+ }
1942
+ }
1943
+ "# ,
1944
+ )
1945
+ }
1813
1946
}
0 commit comments