Skip to content

Refactor validate_scalar to take advantage of type information #53826

Closed
@oli-obk

Description

@oli-obk

Currently we have no way of detecting that

#![feature(const_raw_ptr_deref)]

const X: &i32 = unsafe { &*(8 as *const i32) };

fn main() {}

is UB (an integer is not a valid safe address at compile-time as you can safely dereference it in another constant and then you'd get an error and UB if done at runtime.

The basic idea is to take the function at

fn validate_scalar(
and split it into two parts

  • validate_scalar_by_type which first matches on the scalar's type and then decides how to operate on it
    • the existing match should basically be pulled out and the correctness checks happen after one knows which type one is operating on
    • new arm: ty::Ref can just use to_ptr() on the value and convert the error into a validation_failure! error (see how this is done elsewhere in the same file)
      • do the pointer recursion only here
  • validate_scalar_by_layout which pretty much does everything else that the current validate_scalar does, minus the type checks and pointer recursion.
  • run validate_scalar_by_layout on every scalar (maybe here?), and not just on leaf fields. This is necessary to catch const FOO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(0) }; because right now we're just checking the field of NonZeroU8, which is u8 and thus fine to be 0.

cc @RalfJung

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions