-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Tracking issue for unsafe operations in const fn #55607
Copy link
Copy link
Closed
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.Blocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamRelevant to the language teamdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.Blocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamRelevant to the language teamdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
This is a tracking issue for the RFC "Const functions and inherent methods" (rust-lang/rfcs#911).
This issue only tracks a subset of the proposal in 911 that we are (hopefully) comfortable with stabilizing. To opt into the minimal subset, use
#![feature(min_const_unsafe_fn)]. To use the more expansive feature set, you can continue using#![feature(const_fn)]and other associated feature gates.Currently, while you can write
unsafe {}inside aconst fn/unsafe const fn, it is not possible to actually possible to call any unsafe operations inside the block. This makes it impossible to implement safeconst fnabstractions such asVec::new. This issue builds upon #53555 by allowing you to useunsafeoperations insideconst fnso that we can make more abstractionsconst fn.Exhaustive list of features supported in
const fnwith#![feature(min_const_unsafe_fn)]:NonZero) with#[rustc_layout_scalar_valid_range_start]becomesunsafe. This is an internal bug-fix that has no user facing consequences. A motivation is given in Tracking issue for unsafe operations in const fn #55607 (comment) and in Tracking issue for unsafe operations in const fn #55607 (comment).const unsafe fnfunctions insideconst fnfunctions inside anunsafe { ... }block.const unsafe fnfunctions insideconst unsafe fnfunctions.Non-exhaustive lists of things that don't become allowed with
#![feature(min_const_unsafe_fn)]:Callingconst unsafe fnfunctions directly inside otherconst unsafe fnfunctions.For example:
We impose this restriction because @RalfJung has noted that this is not a good thing inunsafe fnandfn. Thus, for now, we want to avoid making the situation worse inconst unsafe fn. We can lift the restriction later if we want to.EDIT: This restriction has been removed.
Calling
ptr::read,mem::transmuteor other functions that can't be written asconst unsafe fnin user code (see discussion below...).Defererencing raw pointers; Tracked in [tracking issue] dereferencing raw pointers inside constants (const_raw_ptr_deref) #51911.
Union field accesses; Tracked in [tracking issue]
unionfield access insideconst fn#51909.Casting raw pointers to integers
Taking references to fields of packed structs
accessing
extern staticsThings to be done before stabilizing:
min_const_unsafe_fnfeature gate. (Allow callingconst unsafe fninconst fnbehind a feature gate #55635)Unresolved questions:
None.
Vocabulary:
cc #24111.