- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
MinConstGenerics UI test for invalid values for bool & char #78428
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
    
    
  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
        
          
          
            45 changes: 45 additions & 0 deletions
          
          45 
        
  src/test/ui/const-generics/min_const_generics/invalid-patterns.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,45 @@ | ||
| #![feature(min_const_generics)] | ||
| use std::mem::transmute; | ||
|  | ||
| fn get_flag<const FlagSet: bool, const ShortName: char>() -> Option<char> { | ||
| if FlagSet { | ||
| Some(ShortName) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|  | ||
| union CharRaw { | ||
| byte: u8, | ||
| character: char, | ||
| } | ||
|  | ||
| union BoolRaw { | ||
| byte: u8, | ||
| boolean: bool, | ||
| } | ||
|  | ||
| const char_raw: CharRaw = CharRaw { byte: 0xFF }; | ||
| const bool_raw: BoolRaw = BoolRaw { byte: 0x42 }; | ||
|  | ||
| fn main() { | ||
| // Test that basic cases don't work | ||
| assert!(get_flag::<true, 'c'>().is_some()); | ||
| assert!(get_flag::<false, 'x'>().is_none()); | ||
| get_flag::<false, 0xFF>(); | ||
| //~^ ERROR mismatched types | ||
| get_flag::<7, 'c'>(); | ||
| //~^ ERROR mismatched types | ||
| get_flag::<42, 0x5ad>(); | ||
| //~^ ERROR mismatched types | ||
| //~| ERROR mismatched types | ||
|  | ||
|  | ||
| get_flag::<false, { unsafe { char_raw.character } }>(); | ||
| //~^ ERROR it is undefined behavior | ||
| get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>(); | ||
| //~^ ERROR it is undefined behavior | ||
| get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>(); | ||
| //~^ ERROR it is undefined behavior | ||
| //~| ERROR it is undefined behavior | ||
| } | ||
        
          
          
            60 changes: 60 additions & 0 deletions
          
          60 
        
  src/test/ui/const-generics/min_const_generics/invalid-patterns.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,60 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/invalid-patterns.rs:29:21 | ||
| | | ||
| LL | get_flag::<false, 0xFF>(); | ||
| | ^^^^ expected `char`, found `u8` | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/invalid-patterns.rs:31:14 | ||
| | | ||
| LL | get_flag::<7, 'c'>(); | ||
| | ^ expected `bool`, found integer | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/invalid-patterns.rs:33:14 | ||
| | | ||
| LL | get_flag::<42, 0x5ad>(); | ||
| | ^^ expected `bool`, found integer | ||
|  | ||
| error[E0308]: mismatched types | ||
| --> $DIR/invalid-patterns.rs:33:18 | ||
| | | ||
| LL | get_flag::<42, 0x5ad>(); | ||
| | ^^^^^ expected `char`, found `u8` | ||
|  | ||
| error[E0080]: it is undefined behavior to use this value | ||
| --> $DIR/invalid-patterns.rs:38:21 | ||
| | | ||
| LL | get_flag::<false, { unsafe { char_raw.character } }>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) | ||
| | | ||
| = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|  | ||
| error[E0080]: it is undefined behavior to use this value | ||
| --> $DIR/invalid-patterns.rs:40:14 | ||
| | | ||
| LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean | ||
| | | ||
| = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|  | ||
| error[E0080]: it is undefined behavior to use this value | ||
| --> $DIR/invalid-patterns.rs:42:14 | ||
| | | ||
| LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean | ||
| | | ||
| = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|  | ||
| error[E0080]: it is undefined behavior to use this value | ||
| --> $DIR/invalid-patterns.rs:42:47 | ||
| | | ||
| LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>(); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) | ||
| | | ||
| = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|  | ||
| error: aborting due to 8 previous errors | ||
|  | ||
| Some errors have detailed explanations: E0080, E0308. | ||
| For more information about an error, try `rustc --explain E0080`. | 
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.