Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
matthewjasper opened this issue Jun 2, 2019 · 6 comments · Fixed by #65188
Closed
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@matthewjasper
Copy link
Contributor

matthewjasper commented Jun 2, 2019

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

@matthewjasper matthewjasper added T-lang Relevant to the language team, which will review and decide on the PR/issue. A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Jun 2, 2019
@Centril
Copy link
Contributor

Centril commented Jun 15, 2019

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

@Centril Centril added B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. labels Jun 15, 2019
@mexus
Copy link

mexus commented Aug 26, 2019

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

@mati865
Copy link
Contributor

mati865 commented Aug 26, 2019

FCP and stabilization pull request.

@Centril
Copy link
Contributor

Centril commented Aug 26, 2019

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

@matthewjasper
Copy link
Contributor Author

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
Copy link
Contributor

Centril commented Sep 15, 2019

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.

bors added a commit that referenced this issue Oct 27, 2019
…entril

Stabilize `const_constructor`

# 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:

```rust
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);
    <Option<_>>::Some(5);
}
```

### Motivation

Consistency with other `const fn`. Consistency between syntactic path forms.

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

## Tests

* [ui/consts/const_constructor/const-construct-call.rs](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.
* [ui/consts/const_constructor/const_constructor_qpath.rs](https://github.com/rust-lang/rust/blob/1850dfcdabf8258a1f023f26c2c59e96b869dd95/src/test/ui/consts/const_constructor/const_constructor_qpath.rs) - Tests that type qualified paths to enum variants are also considered to be `const fn`.(#64247)

r? @oli-obk

Closes #61456
Closes  #64247
@bors bors closed this as completed in 03a50ae Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants