- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Tweak type inference for const operands in inline asm
          #125558
        
          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
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      4d74712
              
                Tweak type inference for `const` operands in inline asm
              
              
                Amanieu 575fc72
              
                Work around #96304 by ignoring one test case
              
              
                Amanieu 97738e1
              
                apply fix suggested by lcnr
              
              
                folkertdev c77b569
              
                Apply suggestions from code review
              
              
                folkertdev d4ca1ac
              
                rustfmt
              
              
                Amanieu be66415
              
                use `ErrorGuaranteed` from emit
              
              
                folkertdev b73077e
              
                separate test file for invalid sym operand
              
              
                folkertdev 47e6db5
              
                separate test file for invalid const operand
              
              
                folkertdev eb726a5
              
                add `needs-asm-support` to invalid-sym-operand
              
              
                folkertdev 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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,51 @@ | ||
| //@ needs-asm-support | ||
| //@ ignore-nvptx64 | ||
| //@ ignore-spirv | ||
|  | ||
| #![feature(asm_const)] | ||
|  | ||
| use std::arch::{asm, global_asm}; | ||
|  | ||
| // Const operands must be integers and must be constants. | ||
|  | ||
| global_asm!("{}", const 0); | ||
| global_asm!("{}", const 0i32); | ||
| global_asm!("{}", const 0i128); | ||
| global_asm!("{}", const 0f32); | ||
| //~^ ERROR invalid type for `const` operand | ||
| global_asm!("{}", const 0 as *mut u8); | ||
| //~^ ERROR invalid type for `const` operand | ||
|  | ||
| fn main() { | ||
| unsafe { | ||
| // Const operands must be integers and must be constants. | ||
|  | ||
| asm!("{}", const 0); | ||
| asm!("{}", const 0i32); | ||
| asm!("{}", const 0i128); | ||
| asm!("{}", const 0f32); | ||
| //~^ ERROR invalid type for `const` operand | ||
| asm!("{}", const 0 as *mut u8); | ||
| //~^ ERROR invalid type for `const` operand | ||
| asm!("{}", const &0); | ||
| //~^ ERROR invalid type for `const` operand | ||
|  | ||
| // Constants must be... constant | ||
|  | ||
| let x = 0; | ||
| const fn const_foo(x: i32) -> i32 { | ||
| x | ||
| } | ||
| const fn const_bar<T>(x: T) -> T { | ||
| x | ||
| } | ||
| asm!("{}", const x); | ||
| //~^ ERROR attempt to use a non-constant value in a constant | ||
| asm!("{}", const const_foo(0)); | ||
| asm!("{}", const const_foo(x)); | ||
| //~^ ERROR attempt to use a non-constant value in a constant | ||
| asm!("{}", const const_bar(0)); | ||
| asm!("{}", const const_bar(x)); | ||
| //~^ ERROR attempt to use a non-constant value in a constant | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  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.
a suggestion: you can move this check into
fn anon_const_type_ofand return the returned type withty::Errorif it's not an int. THis should also fix the ICE in mir borrowckThere 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.
I tried this here master...folkertdev:rust:const-asm-type-fix and that makes the test work! However, error messages are now emitted out of order. I'm not sure how big a problem that is, and how to fix it if needed.
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.
as an extra note, this issue appears to be entirely unrelated to #96304, lifetimes work fine in these const expressions.
Uh oh!
There was an error while loading. Please reload this page.
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.
👍 yeah, that's my preferred solution, though I'd maybe keep the check in
intrinsicckas an assert in casetype_ofgets changed in the future. I don't think the message order is something we should worry about here.