Skip to content

Under feature(capture_disjoint_fields), unsafe-checking has false positives under unsafe blocks #85561

Description

@pnkfelix

Spawned off of issue #85435

I tried this code on nightly:

#![allow(incomplete_features)] // narrow diagnostic output
#![feature(capture_disjoint_fields)] // need this to witness the bug consistently in rustc's git history.

fn main() {
    let val: u8 = 5;
    let u8_ptr: *const u8 = &val;
    let _closure = || {
        unsafe {
            // Fails compilation with:                                                                                                                                                                                            
            // error[E0133]: dereference of raw pointer is unsafe and                                                                                                                                                             
            //               requires unsafe function or block                                                                                                                                                                    
            let tmp = *u8_ptr;
            tmp

            // Just dereferencing and returning directly compiles fine:                                                                                                                                                           
            // *u8_ptr
        }
    };
}

I expected to see this happen: Code accepted by compiler

Instead, this happened:

error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
  --> src/main.rs:12:23
   |
12 |             let tmp = *u8_ptr;
   |                       ^^^^^^^ dereference of raw pointer
   |
   = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.

Note: This has same root cause as #85435. But, we're going to address #85435 with a targeted short-term fix (namely PR #85564), so that it won't hit stable. So I'm filing this bug to track fixing the root cause.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions