@@ -460,15 +460,11 @@ fn compile_error_expand(
460460 let err = match & * tt. token_trees {
461461 [ tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
462462 symbol : text,
463- span : _ ,
463+ span,
464464 kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) ,
465465 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" ) ,
472468 } ;
473469
474470 ExpandResult { value : quote ! { span =>} , err : Some ( err) }
@@ -478,7 +474,7 @@ fn concat_expand(
478474 _db : & dyn ExpandDatabase ,
479475 _arg_id : MacroCallId ,
480476 tt : & tt:: Subtree ,
481- _ : Span ,
477+ call_site : Span ,
482478) -> ExpandResult < tt:: Subtree > {
483479 let mut err = None ;
484480 let mut text = String :: new ( ) ;
@@ -527,7 +523,9 @@ fn concat_expand(
527523 | tt:: LitKind :: ByteStrRaw ( _)
528524 | tt:: LitKind :: CStr
529525 | 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+ }
531529 }
532530 }
533531 // handle boolean literals
@@ -539,7 +537,7 @@ fn concat_expand(
539537 }
540538 tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
541539 _ => {
542- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
540+ err. get_or_insert ( ExpandError :: other ( call_site , "unexpected token" ) ) ;
543541 }
544542 }
545543 }
@@ -551,7 +549,7 @@ fn concat_bytes_expand(
551549 _db : & dyn ExpandDatabase ,
552550 _arg_id : MacroCallId ,
553551 tt : & tt:: Subtree ,
554- _ : Span ,
552+ call_site : Span ,
555553) -> ExpandResult < tt:: Subtree > {
556554 let mut bytes = String :: new ( ) ;
557555 let mut err = None ;
@@ -585,20 +583,22 @@ fn concat_bytes_expand(
585583 bytes. extend ( text. as_str ( ) . escape_debug ( ) ) ;
586584 }
587585 _ => {
588- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
586+ err. get_or_insert ( ExpandError :: other ( * span , "unexpected token" ) ) ;
589587 break ;
590588 }
591589 }
592590 }
593591 tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
594592 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+ {
596596 err. get_or_insert ( e) ;
597597 break ;
598598 }
599599 }
600600 _ => {
601- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
601+ err. get_or_insert ( ExpandError :: other ( call_site , "unexpected token" ) ) ;
602602 break ;
603603 }
604604 }
@@ -623,6 +623,7 @@ fn concat_bytes_expand_subtree(
623623 tree : & tt:: Subtree ,
624624 bytes : & mut String ,
625625 mut record_span : impl FnMut ( Span ) ,
626+ err_span : Span ,
626627) -> Result < ( ) , ExpandError > {
627628 for ( ti, tt) in tree. token_trees . iter ( ) . enumerate ( ) {
628629 match tt {
@@ -650,7 +651,7 @@ fn concat_bytes_expand_subtree(
650651 }
651652 tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if ti % 2 == 1 && punct. char == ',' => ( ) ,
652653 _ => {
653- return Err ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
654+ return Err ( ExpandError :: other ( err_span , "unexpected token" ) ) ;
654655 }
655656 }
656657 }
@@ -672,7 +673,7 @@ fn concat_idents_expand(
672673 }
673674 tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( punct) ) if i % 2 == 1 && punct. char == ',' => ( ) ,
674675 _ => {
675- err. get_or_insert ( mbe :: ExpandError :: UnexpectedToken . into ( ) ) ;
676+ err. get_or_insert ( ExpandError :: other ( span , "unexpected token" ) ) ;
676677 }
677678 }
678679 }
@@ -686,16 +687,17 @@ fn relative_file(
686687 call_id : MacroCallId ,
687688 path_str : & str ,
688689 allow_recursion : bool ,
690+ err_span : Span ,
689691) -> Result < EditionedFileId , ExpandError > {
690692 let lookup = call_id. lookup ( db) ;
691693 let call_site = lookup. kind . file_id ( ) . original_file_respecting_includes ( db) . file_id ( ) ;
692694 let path = AnchoredPath { anchor : call_site, path : path_str } ;
693695 let res = db
694696 . 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}`" ) ) ) ?;
696698 // Prevent include itself
697699 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}`" ) ) )
699701 } else {
700702 Ok ( EditionedFileId :: new ( res, db. crate_graph ( ) [ lookup. krate ] . edition ) )
701703 }
@@ -727,7 +729,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<(Symbol, Span), ExpandError> {
727729 }
728730 _ => None ,
729731 } )
730- . ok_or ( mbe :: ExpandError :: ConversionError . into ( ) )
732+ . ok_or ( ExpandError :: other ( tt . delimiter . open , "expected string literal" ) )
731733}
732734
733735fn include_expand (
@@ -751,7 +753,7 @@ fn include_expand(
751753 Some ( it) => ExpandResult :: ok ( it) ,
752754 None => ExpandResult :: new (
753755 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" ) ,
755757 ) ,
756758 }
757759}
@@ -761,7 +763,7 @@ pub fn include_input_to_file_id(
761763 arg_id : MacroCallId ,
762764 arg : & tt:: Subtree ,
763765) -> 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 )
765767}
766768
767769fn include_bytes_expand (
@@ -800,7 +802,7 @@ fn include_str_expand(
800802 // it's unusual to `include_str!` a Rust file), but we can return an empty string.
801803 // Ideally, we'd be able to offer a precise expansion if the user asks for macro
802804 // 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 ) {
804806 Ok ( file_id) => file_id,
805807 Err ( _) => {
806808 return ExpandResult :: ok ( quote ! ( span =>"" ) ) ;
@@ -836,7 +838,10 @@ fn env_expand(
836838 // The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid
837839 // unnecessary diagnostics for eg. `CARGO_PKG_NAME`.
838840 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+ ) ) ;
840845 }
841846
842847 // If the variable is unset, still return a dummy string to help type inference along.
@@ -885,7 +890,7 @@ fn quote_expand(
885890) -> ExpandResult < tt:: Subtree > {
886891 ExpandResult :: new (
887892 tt:: Subtree :: empty ( tt:: DelimSpan { open : span, close : span } ) ,
888- ExpandError :: other ( "quote! is not implemented" ) ,
893+ ExpandError :: other ( span , "quote! is not implemented" ) ,
889894 )
890895}
891896
0 commit comments