diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-03-23 22:13:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-23 22:13:24 +0100 |
| commit | 23ef234bf765951e49805fbdad95f538ac33f2d8 (patch) | |
| tree | ba0debae5eaf0939a16023482366193796259f92 /compiler/rustc_const_eval/src | |
| parent | 0c79c862f0e7e6ab56ea6d78b15410de209e0825 (diff) | |
| parent | a76e5b1882f78c250a3f7338d12c559029949d73 (diff) | |
| download | rust-23ef234bf765951e49805fbdad95f538ac33f2d8.tar.gz rust-23ef234bf765951e49805fbdad95f538ac33f2d8.zip | |
Rollup merge of #95221 - RalfJung:check_and_deref_ptr, r=oli-obk
interpret/memory: simplify check_and_deref_ptr *Finally* I saw a way to make this code simpler. The odd preprocessing in `let ptr_or_addr =` has bothered me since forever, but it actually became unnecessary in the last provenance refactoring. :) This also leads to slightly more explicit error messages as a nice side-effect. :tada: r? `@oli-obk`
Diffstat (limited to 'compiler/rustc_const_eval/src')
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/memory.rs | 20 |
1 files changed, 5 insertions, 15 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 6397fcaaf8d..871b7578abd 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -427,22 +427,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { } } - // Extract from the pointer an `Option<AllocId>` and an offset, which is relative to the - // allocation or (if that is `None`) an absolute address. - let ptr_or_addr = if size.bytes() == 0 { - // Let's see what we can do, but don't throw errors if there's nothing there. - self.ptr_try_get_alloc(ptr) - } else { - // A "real" access, we insist on getting an `AllocId`. - Ok(self.ptr_get_alloc(ptr)?) - }; - Ok(match ptr_or_addr { + Ok(match self.ptr_try_get_alloc(ptr) { Err(addr) => { - // No memory is actually being accessed. - debug_assert!(size.bytes() == 0); - // Must be non-null. - if addr == 0 { - throw_ub!(DanglingIntPointer(0, msg)) + // We couldn't get a proper allocation. This is only okay if the access size is 0, + // and the address is not null. + if size.bytes() > 0 || addr == 0 { + throw_ub!(DanglingIntPointer(addr, msg)); } // Must be aligned. if let Some(align) = align { |
