Skip to content

Tracking issue for treating tuple struct and variant constructors as const fn #61456

Closed
@matthewjasper

Description

@matthewjasper
Contributor

The const_constructor feature allows calling any expression of a tuple-like constructor in a const fn:

const fn make_options() {
    Some(0); // OK on stable
    (Option::Some)(1); // OK on stable
    let f = Option::Some;
    f(2); // Requires feature gate
}

cc @oli-obk @Centril

Activity

added
T-langRelevant to the language team
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Jun 2, 2019
Centril

Centril commented on Jun 15, 2019

@Centril
Contributor

Implemented on 2019-06-07 by @matthewjasper in #61209 which was reviewed by @oli-obk

added
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.
B-unstableBlocker: Implemented in the nightly compiler and unstable.
on Jun 15, 2019
mexus

mexus commented on Aug 26, 2019

@mexus

Is there anything else needs to be done to resolve this issue?

mati865

mati865 commented on Aug 26, 2019

@mati865
Member

FCP and stabilization pull request.

Centril

Centril commented on Aug 26, 2019

@Centril
Contributor

@matthewjasper Do you think you have time to write a report (e.g. in the style of #61682) and amend the reference after?

matthewjasper

matthewjasper commented on Sep 15, 2019

@matthewjasper
ContributorAuthor

Stabilization proposal

I propose that we stabilize #![feature(const_constructor)].

Tracking issue: #61456
Version target: 1.40 (2019-11-05 => beta, 2019-12-19 => stable).

What is stabilized

User guide

Tuple struct and tuple variant constructors are now considered to be constant functions. As such a call expression where the callee has a tuple struct or variant constructor "function item" type can be called :

const fn make_options() {
    // These already work because they are special cased:
    Some(0);
    (Option::Some)(1);
    // These also work now:
    let f = Option::Some;
    f(2);
    {Option::Some}(3);
}

Motivation

Consistency with other const fn.

This should also ensure that constructors implement const Fn traits and can be coerced to const fn function pointers, if they are introduced.

Test

https://github.com/rust-lang/rust/blob/0d75ab2293a106eb674ac01860910cfc1580837e/src/test/ui/consts/const_constructor/const-construct-call.rs - Tests various syntactic forms, use in both const fn and const items, and constructors in both the current and extern crates.

The case in #64247 should also get a test.

Centril

Centril commented on Sep 15, 2019

@Centril
Contributor

The case in #64247 should also get a test.

@matthewjasper Thanks! Could you embed this report in a PR with the aforementioned test added there as well? I'll FCP that PR.

added 2 commits that reference this issue on Oct 27, 2019

Auto merge of #65188 - matthewjasper:stabilize-const-constructor, r=C…

Auto merge of #65188 - matthewjasper:stabilize-const-constructor, r=C…

added
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
on Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @RalfJung@Centril@mati865@mexus@matthewjasper

      Issue actions

        Tracking issue for treating tuple struct and variant constructors as const fn · Issue #61456 · rust-lang/rust