@@ -460,15 +460,11 @@ fn compile_error_expand(
460
460
let err = match & * tt. token_trees {
461
461
[ tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
462
462
symbol : text,
463
- span : _ ,
463
+ span,
464
464
kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) ,
465
465
suffix : _,
466
- } ) ) ] =>
467
- // FIXME: Use the span here!
468
- {
469
- ExpandError :: other ( Box :: from ( unescape_str ( text) . as_str ( ) ) )
470
- }
471
- _ => ExpandError :: other ( "`compile_error!` argument must be a string" ) ,
466
+ } ) ) ] => ExpandError :: other ( * span, Box :: from ( unescape_str ( text) . as_str ( ) ) ) ,
467
+ _ => ExpandError :: other ( span, "`compile_error!` argument must be a string" ) ,
472
468
} ;
473
469
474
470
ExpandResult { value : quote ! { span =>} , err : Some ( err) }
@@ -478,7 +474,7 @@ fn concat_expand(
478
474
_db : & dyn ExpandDatabase ,
479
475
_arg_id : MacroCallId ,
480
476
tt : & tt:: Subtree ,
481
- _ : Span ,
477
+ call_site : Span ,
482
478
) -> ExpandResult < tt:: Subtree > {
483
479
let mut err = None ;
484
480
let mut text = String :: new ( ) ;
@@ -527,7 +523,9 @@ fn concat_expand(
527
523
| tt:: LitKind :: ByteStrRaw ( _)
528
524
| tt:: LitKind :: CStr
529
525
| tt:: LitKind :: CStrRaw ( _)
530
- | tt:: LitKind :: Err ( _) => err = Some ( ExpandError :: other ( "unexpected literal" ) ) ,
526
+ | tt:: LitKind :: Err ( _) => {
527
+ err = Some ( ExpandError :: other ( it. span , "unexpected literal" ) )
528
+ }
531
529
}
532
530
}
533
531
// handle boolean literals
@@ -539,7 +537,7 @@ fn concat_expand(
539
537
}
540
538
tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
541
539
_ => {
542
- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
540
+ err. get_or_insert ( ExpandError :: other ( call_site , "unexpected token" ) ) ;
543
541
}
544
542
}
545
543
}
@@ -551,7 +549,7 @@ fn concat_bytes_expand(
551
549
_db : & dyn ExpandDatabase ,
552
550
_arg_id : MacroCallId ,
553
551
tt : & tt:: Subtree ,
554
- _ : Span ,
552
+ call_site : Span ,
555
553
) -> ExpandResult < tt:: Subtree > {
556
554
let mut bytes = String :: new ( ) ;
557
555
let mut err = None ;
@@ -585,20 +583,22 @@ fn concat_bytes_expand(
585
583
bytes. extend ( text. as_str ( ) . escape_debug ( ) ) ;
586
584
}
587
585
_ => {
588
- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
586
+ err. get_or_insert ( ExpandError :: other ( * span , "unexpected token" ) ) ;
589
587
break ;
590
588
}
591
589
}
592
590
}
593
591
tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
594
592
tt:: TokenTree :: Subtree ( tree) if tree. delimiter . kind == tt:: DelimiterKind :: Bracket => {
595
- if let Err ( e) = concat_bytes_expand_subtree ( tree, & mut bytes, & mut record_span) {
593
+ if let Err ( e) =
594
+ concat_bytes_expand_subtree ( tree, & mut bytes, & mut record_span, call_site)
595
+ {
596
596
err. get_or_insert ( e) ;
597
597
break ;
598
598
}
599
599
}
600
600
_ => {
601
- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
601
+ err. get_or_insert ( ExpandError :: other ( call_site , "unexpected token" ) ) ;
602
602
break ;
603
603
}
604
604
}
@@ -623,6 +623,7 @@ fn concat_bytes_expand_subtree(
623
623
tree : & tt:: Subtree ,
624
624
bytes : & mut String ,
625
625
mut record_span : impl FnMut ( Span ) ,
626
+ err_span : Span ,
626
627
) -> Result < ( ) , ExpandError > {
627
628
for ( ti, tt) in tree. token_trees . iter ( ) . enumerate ( ) {
628
629
match tt {
@@ -650,7 +651,7 @@ fn concat_bytes_expand_subtree(
650
651
}
651
652
tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if ti % 2 == 1 && punct. char == ',' => ( ) ,
652
653
_ => {
653
- return Err ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
654
+ return Err ( ExpandError :: other ( err_span , "unexpected token" ) ) ;
654
655
}
655
656
}
656
657
}
@@ -672,7 +673,7 @@ fn concat_idents_expand(
672
673
}
673
674
tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
674
675
_ => {
675
- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
676
+ err. get_or_insert ( ExpandError :: other ( span , "unexpected token" ) ) ;
676
677
}
677
678
}
678
679
}
@@ -686,16 +687,17 @@ fn relative_file(
686
687
call_id : MacroCallId ,
687
688
path_str : & str ,
688
689
allow_recursion : bool ,
690
+ err_span : Span ,
689
691
) -> Result < EditionedFileId , ExpandError > {
690
692
let lookup = call_id. lookup ( db) ;
691
693
let call_site = lookup. kind . file_id ( ) . original_file_respecting_includes ( db) . file_id ( ) ;
692
694
let path = AnchoredPath { anchor : call_site, path : path_str } ;
693
695
let res = db
694
696
. resolve_path ( path)
695
- . ok_or_else ( || ExpandError :: other ( format ! ( "failed to load file `{path_str}`" ) ) ) ?;
697
+ . ok_or_else ( || ExpandError :: other ( err_span , format ! ( "failed to load file `{path_str}`" ) ) ) ?;
696
698
// Prevent include itself
697
699
if res == call_site && !allow_recursion {
698
- Err ( ExpandError :: other ( format ! ( "recursive inclusion of `{path_str}`" ) ) )
700
+ Err ( ExpandError :: other ( err_span , format ! ( "recursive inclusion of `{path_str}`" ) ) )
699
701
} else {
700
702
Ok ( EditionedFileId :: new ( res, db. crate_graph ( ) [ lookup. krate ] . edition ) )
701
703
}
@@ -727,7 +729,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<(Symbol, Span), ExpandError> {
727
729
}
728
730
_ => None ,
729
731
} )
730
- . ok_or ( mbe :: ExpandError :: ConversionError . into ( ) )
732
+ . ok_or ( ExpandError :: other ( tt . delimiter . open , "expected string literal" ) )
731
733
}
732
734
733
735
fn include_expand (
@@ -751,7 +753,7 @@ fn include_expand(
751
753
Some ( it) => ExpandResult :: ok ( it) ,
752
754
None => ExpandResult :: new (
753
755
tt:: Subtree :: empty ( DelimSpan { open : span, close : span } ) ,
754
- ExpandError :: other ( "failed to parse included file" ) ,
756
+ ExpandError :: other ( span , "failed to parse included file" ) ,
755
757
) ,
756
758
}
757
759
}
@@ -761,7 +763,7 @@ pub fn include_input_to_file_id(
761
763
arg_id : MacroCallId ,
762
764
arg : & tt:: Subtree ,
763
765
) -> Result < EditionedFileId , ExpandError > {
764
- relative_file ( db, arg_id, parse_string ( arg) ?. 0 . as_str ( ) , false )
766
+ relative_file ( db, arg_id, parse_string ( arg) ?. 0 . as_str ( ) , false , arg . delimiter . open )
765
767
}
766
768
767
769
fn include_bytes_expand (
@@ -800,7 +802,7 @@ fn include_str_expand(
800
802
// it's unusual to `include_str!` a Rust file), but we can return an empty string.
801
803
// Ideally, we'd be able to offer a precise expansion if the user asks for macro
802
804
// expansion.
803
- let file_id = match relative_file ( db, arg_id, path. as_str ( ) , true ) {
805
+ let file_id = match relative_file ( db, arg_id, path. as_str ( ) , true , span ) {
804
806
Ok ( file_id) => file_id,
805
807
Err ( _) => {
806
808
return ExpandResult :: ok ( quote ! ( span =>"" ) ) ;
@@ -836,7 +838,10 @@ fn env_expand(
836
838
// The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid
837
839
// unnecessary diagnostics for eg. `CARGO_PKG_NAME`.
838
840
if key. as_str ( ) == "OUT_DIR" {
839
- err = Some ( ExpandError :: other ( r#"`OUT_DIR` not set, enable "build scripts" to fix"# ) ) ;
841
+ err = Some ( ExpandError :: other (
842
+ span,
843
+ r#"`OUT_DIR` not set, enable "build scripts" to fix"# ,
844
+ ) ) ;
840
845
}
841
846
842
847
// If the variable is unset, still return a dummy string to help type inference along.
@@ -885,7 +890,7 @@ fn quote_expand(
885
890
) -> ExpandResult < tt:: Subtree > {
886
891
ExpandResult :: new (
887
892
tt:: Subtree :: empty ( tt:: DelimSpan { open : span, close : span } ) ,
888
- ExpandError :: other ( "quote! is not implemented" ) ,
893
+ ExpandError :: other ( span , "quote! is not implemented" ) ,
889
894
)
890
895
}
891
896
0 commit comments