- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
disallow non-static lifetimes in const generics #74051
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
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
    
  
  
    
              
  
    
      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,23 @@ | ||
| A non-`'static` lifetime was used in a const generic. This is currently not | ||
| allowed. | ||
| 
     | 
||
| Erroneous code example: | ||
| 
     | 
||
| ```compile_fail,E0771 | ||
| #![feature(const_generics)] | ||
| 
     | 
||
| fn function_with_str<'a, const STRING: &'a str>() {} // error! | ||
| ``` | ||
| 
     | 
||
| To fix this issue, the lifetime in the const generic need to be changed to | ||
| `'static`: | ||
| 
     | 
||
| ``` | ||
| #![feature(const_generics)] | ||
| 
     | 
||
| fn function_with_str<const STRING: &'static str>() {} // ok! | ||
| ``` | ||
| 
     | 
||
| For more information, see [GitHub issue #74052]. | ||
| 
     | 
||
| [GitHub issue #74052]: https://github.com/rust-lang/rust/issues/74052 | 
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            17 changes: 17 additions & 0 deletions
          
          17 
        
  src/test/ui/const-generics/const-argument-non-static-lifetime.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,17 @@ | ||
| // run-pass | ||
| 
     | 
||
| #![feature(const_generics)] | ||
| //~^ WARN the feature `const_generics` is incomplete | ||
| #![allow(dead_code)] | ||
| 
     | 
||
| fn test<const N: usize>() {} | ||
| 
     | 
||
| fn wow<'a>() -> &'a () { | ||
| test::<{ | ||
| let _: &'a (); | ||
| 3 | ||
| }>(); | ||
| &() | ||
| } | ||
| 
     | 
||
| fn main() {} | 
        
          
          
            11 changes: 11 additions & 0 deletions
          
          11 
        
  src/test/ui/const-generics/const-argument-non-static-lifetime.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,11 @@ | ||
| warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes | ||
| --> $DIR/const-argument-non-static-lifetime.rs:3:12 | ||
| | | ||
| LL | #![feature(const_generics)] | ||
| | ^^^^^^^^^^^^^^ | ||
| | | ||
| = note: `#[warn(incomplete_features)]` on by default | ||
| = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information | ||
| 
     | 
||
| warning: 1 warning emitted | ||
| 
     | 
  
    
      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,8 @@ | ||
| #![feature(const_generics)] | ||
| //~^ WARN the feature `const_generics` is incomplete | ||
| 
     | 
||
| fn function_with_str<'a, const STRING: &'a str>() {} //~ ERROR E0771 | ||
| 
     | 
||
| fn main() { | ||
| function_with_str::<"Hello, world!">() | ||
| } | 
  
    
      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,20 @@ | ||
| warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes | ||
| --> $DIR/E0771.rs:1:12 | ||
| | | ||
| LL | #![feature(const_generics)] | ||
| | ^^^^^^^^^^^^^^ | ||
| | | ||
| = note: `#[warn(incomplete_features)]` on by default | ||
| = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information | ||
| 
     | 
||
| error[E0771]: use of non-static lifetime `'a` in const generic | ||
| --> $DIR/E0771.rs:4:41 | ||
| | | ||
| LL | fn function_with_str<'a, const STRING: &'a str>() {} | ||
| | ^^ | ||
| | | ||
| = note: for more information, see issue #74052 <https://github.com/rust-lang/rust/issues/74052> | ||
| 
     | 
||
| error: aborting due to previous error; 1 warning emitted | ||
| 
     | 
||
| For more information about this error, try `rustc --explain E0771`. | 
  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.
Hmm, I guess we don't have a
RegionKind::Errorto use here... unfortunate. I don't really remember what happens is we fail to store some kind of resolution for a lifetime in type check -- are we relying on not running type check here? Ah, ok, we insert'staticor an inference variable:rust/src/librustc_typeck/astconv.rs
Lines 202 to 213 in 7138410
I guess that's ok.