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

Announce if and match in constants on nightly #461

Merged
merged 16 commits into from
Nov 25, 2019

Conversation

ecstatic-morse
Copy link
Contributor

This is a rough draft of a post announcing that if and match will be available on nightly. Obviously this can't be merged before rust-lang/rust#66507.

cc @rust-lang/wg-const-eval

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

This change will allow a great number of standard library functions to be
made `const`. If you like, you can help with this process! There's a [list of
numeric functions][const-int] that can be constified with little effort. Be
aware that things move slowly in the standard library, and it's always best
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can be quite liberal with accepting const fn-ification on nightly -- stabilizing is a different matter tho. I think it's in our interest to use the standard library as a proving ground for the feature. So I think we should be more encouraging than "slowly" suggests wrt. unstable stuff.

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constification PRs have been closed because they don't have a motivating use case. I agree with your position on this, but is there consensus? Regardless, "slowly" is unnecessarily pejorative, and I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe in the case you mention, it's more the case that we know that it is not useful in const contexts yet rather than requiring an elaborate justification. Here I think "proving out the language feature" is a useful justification in-and-of-itself. The libs team will have an opportunity to reject stabilization and revert things (if e.g. it incurred hacks), so I think we should be good. My interest here is to have a light-weight process and avoid bureaucracy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've removed that sentence entirely. Do we want to suggest including a concrete example? For many functions that will be overkill.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could give an example of how you constify something by pointing to some merged PR which introduced rustc_const_unstable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that works; rust-lang/rust#61635 is another good example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used rust-lang/rust#61635 as the example and explicitly mention rustc_const_unstable.

`debug_assert_ne`) remain forbidden, since they need to call `Debug::fmt` on
their arguments.

Also forbidden are looping constructs, `while`, `for`, and `loop`, which will
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also note that for loops need trait methods, too and that you can emulate loops with recursion as shown above


## What took you so long?

[Miri], which rust uses under the hood for compile-time function evaluation,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not quite right. Both const eval and miri use the miri engine. The miri engine is essentially a VM that runs MIR as "bytecode". But it's right in the sense that miri showcases what you can do when you take off the safety padding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So change "Miri" to "miri engine"? Also, how do we capitalize miri at the start of a sentence?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh. I think it.s even supposed to be capitalized in the middle of sentences. I do forget. cc @solson

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've typically used "Miri" when referring to the project and miri when referring to the binary or crate name, but I probably haven't been consistent myself.

}
```

However, it is sometimes okay for a `const` to contain a *type* that allows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also OK for constants to have their base value have interior mutability since each use site creates its own copy of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a const to contain a reference to a type that..."?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's good

inside the `const fn`, we don't detect that all fields of `tuple` have been moved
out of, and thus conservatively assume that the drop impl for `tuple` will run.
While this particular case is trivial, there are other, more complex ones that
would require a more expensive solution. It is an open question how precise we
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe note that the solution would make cases not needing it also more expensive

@ecstatic-morse
Copy link
Contributor Author

@oli-obk Pushed a commit addressing each of your concerns.

Co-Authored-By: lqd <remy.rakic+github@gmail.com>
@ecstatic-morse
Copy link
Contributor Author

@nikomatsakis (or anybody else with merge rights) #![feature(const_if_match)] is now available on the latest nightly, so we should publish this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants