- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Fix suggestion spans for expr from macro expansions #112043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            bors
  merged 1 commit into
  rust-lang:master
from
jieyouxu:suggestion_macro_expansion_source_callsites
  
      
      
   
  Aug 3, 2023 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            55 changes: 55 additions & 0 deletions
          
          55 
        
  tests/ui/typeck/issue-110017-format-into-help-deletes-macro.fixed
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // run-rustfix | ||
| #![allow(dead_code)] | ||
|  | ||
| pub fn foo(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(format!("error: {x}").into()) | ||
| //~^ ERROR mismatched types | ||
| } | ||
|  | ||
| macro_rules! outer { | ||
| ($x: expr) => { | ||
| inner!($x) | ||
| } | ||
| } | ||
|  | ||
| macro_rules! inner { | ||
| ($x: expr) => { | ||
| format!("error: {}", $x).into() | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| fn bar(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(outer!(x)) | ||
| } | ||
|  | ||
| macro_rules! entire_fn_outer { | ||
| () => { | ||
| entire_fn!(); | ||
| } | ||
| } | ||
|  | ||
| macro_rules! entire_fn { | ||
| () => { | ||
| pub fn baz(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(format!("error: {x}").into()) | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
| } | ||
|  | ||
| entire_fn_outer!(); | ||
|  | ||
| macro_rules! nontrivial { | ||
| ($x: expr) => { | ||
| Err(format!("error: {}", $x).into()) | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| pub fn qux(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| nontrivial!(x) | ||
| } | ||
|  | ||
|  | ||
| fn main() {} | 
        
          
          
            55 changes: 55 additions & 0 deletions
          
          55 
        
  tests/ui/typeck/issue-110017-format-into-help-deletes-macro.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // run-rustfix | ||
| #![allow(dead_code)] | ||
|  | ||
| pub fn foo(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(format!("error: {x}")) | ||
| //~^ ERROR mismatched types | ||
| } | ||
|  | ||
| macro_rules! outer { | ||
| ($x: expr) => { | ||
| inner!($x) | ||
| } | ||
| } | ||
|  | ||
| macro_rules! inner { | ||
| ($x: expr) => { | ||
| format!("error: {}", $x) | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| fn bar(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(outer!(x)) | ||
| } | ||
|  | ||
| macro_rules! entire_fn_outer { | ||
| () => { | ||
| entire_fn!(); | ||
| } | ||
| } | ||
|  | ||
| macro_rules! entire_fn { | ||
| () => { | ||
| pub fn baz(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| Err(format!("error: {x}")) | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
| } | ||
|  | ||
| entire_fn_outer!(); | ||
|  | ||
| macro_rules! nontrivial { | ||
| ($x: expr) => { | ||
| Err(format!("error: {}", $x)) | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| pub fn qux(x: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
| nontrivial!(x) | ||
| } | ||
|  | ||
|  | ||
| fn main() {} | 
        
          
          
            59 changes: 59 additions & 0 deletions
          
          59 
        
  tests/ui/typeck/issue-110017-format-into-help-deletes-macro.stderr
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-110017-format-into-help-deletes-macro.rs:5:10 | ||
| | | ||
| LL | Err(format!("error: {x}")) | ||
| | ^^^^^^^^^^^^^^^^^^^^^ expected `Box<dyn Error>`, found `String` | ||
| | | ||
| = note: expected struct `Box<dyn std::error::Error>` | ||
| found struct `String` | ||
| = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>` | ||
| | | ||
| LL | Err(format!("error: {x}").into()) | ||
| | +++++++ | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-110017-format-into-help-deletes-macro.rs:23:10 | ||
| | | ||
| LL | Err(outer!(x)) | ||
| | ^^^^^^^^^ expected `Box<dyn Error>`, found `String` | ||
| | | ||
| = note: expected struct `Box<dyn std::error::Error>` | ||
| found struct `String` | ||
| = note: this error originates in the macro `format` which comes from the expansion of the macro `outer` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>` | ||
| | | ||
| LL | format!("error: {}", $x).into() | ||
| | +++++++ | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-110017-format-into-help-deletes-macro.rs:41:2 | ||
| | | ||
| LL | entire_fn_outer!(); | ||
| | ^^^^^^^^^^^^^^^^^^ expected `Box<dyn Error>`, found `String` | ||
| | | ||
| = note: expected struct `Box<dyn std::error::Error>` | ||
| found struct `String` | ||
| = note: this error originates in the macro `format` which comes from the expansion of the macro `entire_fn_outer` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>` | ||
| | | ||
| LL | Err(format!("error: {x}").into()) | ||
| | +++++++ | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-110017-format-into-help-deletes-macro.rs:51:5 | ||
| | | ||
| LL | nontrivial!(x) | ||
| | ^^^^^^^^^^^^^^ expected `Box<dyn Error>`, found `String` | ||
| | | ||
| = note: expected struct `Box<dyn std::error::Error>` | ||
| found struct `String` | ||
| = note: this error originates in the macro `format` which comes from the expansion of the macro `nontrivial` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: call `Into::into` on this expression to convert `String` into `Box<dyn std::error::Error>` | ||
| | | ||
| LL | Err(format!("error: {}", $x).into()) | ||
| | +++++++ | ||
|  | ||
| error: aborting due to 4 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0308`. | 
        
          
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.fixed
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // run-rustfix | ||
| #![allow(dead_code)] | ||
|  | ||
| // https://github.com/rust-lang/rust/issues/112007 | ||
| fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result { | ||
| if true { | ||
| writeln!(w, "`;?` here ->")?; | ||
| } else { | ||
| return writeln!(w, "but not here"); | ||
| //~^ ERROR mismatched types | ||
| }; | ||
| Ok(()) | ||
| } | ||
|  | ||
| macro_rules! baz { | ||
| ($w: expr) => { | ||
| bar!($w) | ||
| } | ||
| } | ||
|  | ||
| macro_rules! bar { | ||
| ($w: expr) => { | ||
| writeln!($w, "but not here") | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| fn foo<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result { | ||
| if true { | ||
| writeln!(w, "`;?` here ->")?; | ||
| } else { | ||
| return baz!(w); | ||
| }; | ||
| Ok(()) | ||
| } | ||
|  | ||
| fn main() {} | 
        
          
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // run-rustfix | ||
| #![allow(dead_code)] | ||
|  | ||
| // https://github.com/rust-lang/rust/issues/112007 | ||
| fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result { | ||
| if true { | ||
| writeln!(w, "`;?` here ->")?; | ||
| } else { | ||
| writeln!(w, "but not here") | ||
| //~^ ERROR mismatched types | ||
| } | ||
| Ok(()) | ||
| } | ||
|  | ||
| macro_rules! baz { | ||
| ($w: expr) => { | ||
| bar!($w) | ||
| } | ||
| } | ||
|  | ||
| macro_rules! bar { | ||
| ($w: expr) => { | ||
| writeln!($w, "but not here") | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|  | ||
| fn foo<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result { | ||
| if true { | ||
| writeln!(w, "`;?` here ->")?; | ||
| } else { | ||
| baz!(w) | ||
| } | ||
| Ok(()) | ||
| } | ||
|  | ||
| fn main() {} | 
        
          
          
            50 changes: 50 additions & 0 deletions
          
          50 
        
  tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.stderr
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-112007-leaked-writeln-macro-internals.rs:9:9 | ||
| | | ||
| LL | / if true { | ||
| LL | | writeln!(w, "`;?` here ->")?; | ||
| LL | | } else { | ||
| LL | | writeln!(w, "but not here") | ||
| | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>` | ||
| LL | | | ||
| LL | | } | ||
| | |_____- expected this to be `()` | ||
| | | ||
| = note: expected unit type `()` | ||
| found enum `Result<(), std::fmt::Error>` | ||
| = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: consider using a semicolon here | ||
| | | ||
| LL | }; | ||
| | + | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | return writeln!(w, "but not here"); | ||
| | ++++++ + | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/issue-112007-leaked-writeln-macro-internals.rs:32:9 | ||
| | | ||
| LL | / if true { | ||
| LL | | writeln!(w, "`;?` here ->")?; | ||
| LL | | } else { | ||
| LL | | baz!(w) | ||
| | | ^^^^^^^ expected `()`, found `Result<(), Error>` | ||
| LL | | } | ||
| | |_____- expected this to be `()` | ||
| | | ||
| = note: expected unit type `()` | ||
| found enum `Result<(), std::fmt::Error>` | ||
| = note: this error originates in the macro `writeln` which comes from the expansion of the macro `baz` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: consider using a semicolon here | ||
| | | ||
| LL | }; | ||
| | + | ||
| help: you might have meant to return this value | ||
| | | ||
| LL | return baz!(w); | ||
| | ++++++ + | ||
|  | ||
| error: aborting due to 2 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0308`. | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this different than
Span::find_ancestor_in_same_ctxt?