- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Add intrinsics::const_deallocate
          #92274
        
          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
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      aa6795e
              
                Add `intrinsics::const_deallocate`
              
              
                lilasta 29932db
              
                `const_deallocate`: Don't deallocate memory allocated in an another c…
              
              
                lilasta 7a7144f
              
                test_const_allocate_at_runtime
              
              
                lilasta 9728cc4
              
                Document about some behaviors of `const_(de)allocate` and add some te…
              
              
                lilasta 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            16 changes: 16 additions & 0 deletions
          
          16 
        
  src/test/ui/consts/const-eval/heap/alloc_intrinsic_zero_sized.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,16 @@ | ||
| // run-pass | ||
| #![feature(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
| #![feature(inline_const)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| struct ZST; | ||
|  | ||
| fn main() { | ||
| const { | ||
| unsafe { | ||
| let _ = intrinsics::const_allocate(0, 0) as *mut ZST; | ||
| } | ||
| } | ||
| } | 
  
    
      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,36 @@ | ||
| // run-pass | ||
| #![feature(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
| #![feature(const_mut_refs)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| const _X: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 4); | ||
| }; | ||
|  | ||
| const Y: &u32 = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4) as *mut u32; | ||
| *ptr = 42; | ||
| &*ptr | ||
| }; | ||
|  | ||
| const Z: &u32 = &42; | ||
|  | ||
| const _Z: () = unsafe { | ||
| let ptr1 = Y as *const _ as *mut u8; | ||
| intrinsics::const_deallocate(ptr1, 4, 4); // nop | ||
| intrinsics::const_deallocate(ptr1, 2, 4); // nop | ||
| intrinsics::const_deallocate(ptr1, 4, 2); // nop | ||
|  | ||
| let ptr2 = Z as *const _ as *mut u8; | ||
| intrinsics::const_deallocate(ptr2, 4, 4); // nop | ||
| intrinsics::const_deallocate(ptr2, 2, 4); // nop | ||
| intrinsics::const_deallocate(ptr2, 4, 2); // nop | ||
| }; | ||
|  | ||
| fn main() { | ||
| assert_eq!(*Y, 42); | ||
| assert_eq!(*Z, 42); | ||
| } | 
        
          
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_dangling.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,22 @@ | ||
| #![feature(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
| #![feature(const_mut_refs)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| const _X: &'static u8 = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 4); | ||
| &*ptr | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| const _Y: u8 = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| let reference = &*ptr; | ||
| intrinsics::const_deallocate(ptr, 4, 4); | ||
| *reference | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| fn main() {} | 
        
          
          
            15 changes: 15 additions & 0 deletions
          
          15 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_dangling.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,15 @@ | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_dangling.rs:10:5 | ||
| | | ||
| LL | &*ptr | ||
| | ^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|  | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_dangling.rs:18:5 | ||
| | | ||
| LL | *reference | ||
| | ^^^^^^^^^^ pointer to alloc4 was dereferenced after this allocation got freed | ||
|  | ||
| error: aborting due to 2 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0080`. | 
        
          
          
            13 changes: 13 additions & 0 deletions
          
          13 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_duplicate.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,13 @@ | ||
| #![feature(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| const _X: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 4); | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| fn main() {} | 
        
          
          
            9 changes: 9 additions & 0 deletions
          
          9 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_duplicate.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,9 @@ | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_duplicate.rs:9:5 | ||
| | | ||
| LL | intrinsics::const_deallocate(ptr, 4, 4); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed | ||
|  | ||
| error: aborting due to previous error | ||
|  | ||
| For more information about this error, try `rustc --explain E0080`. | 
        
          
          
            29 changes: 29 additions & 0 deletions
          
          29 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.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,29 @@ | ||
| #![feature(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| const _X: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 2); | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
| const _Y: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 2, 4); | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| const _Z: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 3, 4); | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| const _W: () = unsafe { | ||
| let ptr = intrinsics::const_allocate(4, 4); | ||
| intrinsics::const_deallocate(ptr, 4, 3); | ||
| //~^ error: evaluation of constant value failed | ||
| }; | ||
|  | ||
| fn main() {} | 
        
          
          
            27 changes: 27 additions & 0 deletions
          
          27 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_incorrect_layout.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,27 @@ | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_incorrect_layout.rs:8:5 | ||
| | | ||
| LL | intrinsics::const_deallocate(ptr, 4, 2); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc2 has size 4 and alignment 4, but gave size 4 and alignment 2 | ||
|  | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_incorrect_layout.rs:13:5 | ||
| | | ||
| LL | intrinsics::const_deallocate(ptr, 2, 4); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc4 has size 4 and alignment 4, but gave size 2 and alignment 4 | ||
|  | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_incorrect_layout.rs:19:5 | ||
| | | ||
| LL | intrinsics::const_deallocate(ptr, 3, 4); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect layout on deallocation: alloc6 has size 4 and alignment 4, but gave size 3 and alignment 4 | ||
|  | ||
| error[E0080]: evaluation of constant value failed | ||
| --> $DIR/dealloc_intrinsic_incorrect_layout.rs:25:5 | ||
| | | ||
| LL | intrinsics::const_deallocate(ptr, 4, 3); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ align has to be a power of 2, `3` is not a power of 2 | ||
|  | ||
| error: aborting due to 4 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0080`. | 
        
          
          
            17 changes: 17 additions & 0 deletions
          
          17 
        
  src/test/ui/consts/const-eval/heap/dealloc_intrinsic_zero_sized.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(core_intrinsics)] | ||
| #![feature(const_heap)] | ||
| #![feature(inline_const)] | ||
|  | ||
| use std::intrinsics; | ||
|  | ||
| fn main() { | ||
| const { | ||
| unsafe { | ||
| let ptr1 = intrinsics::const_allocate(0, 0); | ||
| let ptr2 = intrinsics::const_allocate(0, 0); | ||
| intrinsics::const_deallocate(ptr1, 0, 0); | ||
| intrinsics::const_deallocate(ptr2, 0, 0); | ||
| } | ||
| } | ||
| } | 
  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.