Tracking Issue for the use of ? in constants
The ? operator expands to invoking Try::into_result for the argument and Into::into for the error of the result. In order to support these, we need to
- stabilize
impl const Trait for Typeitems https://github.com/rust-lang/rust/issues/67792 - implement
const TryforResultandOptionin libstd - users need to implement
const Intofor their error types- Since we have an
impl<T> From<T> for T, we have to make this a const impl, too, as well as theimpl<T: From<U>, U> Into<T> for Uimpl
- Since we have an
The
?operator expands to invokingTry::into_resultfor the argument andInto::intofor the error of the result. In order to support these, we need to- Figure out traits-in-const: https://github.com/rust-lang/rust/issues/110395
- stabilize
impl const Trait for Typeitems https://github.com/rust-lang/rust/issues/67792 - implement
const TryforResultandOptionin libstd - users need to implement
const Intofor their error types- Since we have an
impl<T> From<T> for T, we have to make this a const impl, too, as well as theimpl<T: From<U>, U> Into<T> for Uimpl
- Since we have an
Ralf Jung at 2024-09-08 07:06:15
#60964 also needs to be resolved to be able to implement
const Try. See https://play.rust-lang.org/?version=nightly&gist=bdf8f8e2774f54c084e2cb25624ce193Alphyr at 2020-07-30 13:35:37
Would it be reasonable to permit
?inconst fnonly forOption<T>for now? The only thing would have to change is the desugaring, I think.Jacob Pratt at 2022-03-01 04:23:58
You mean to make the desugaring different if we're in a const fn?
Oli Scherer at 2022-03-01 06:26:23
I don't see any reason we couldn't desugar to this unconditionally for
Option<T>:match opt { Some(val) => val, None => return None, }Sure, the
Trytrait was designed with this in mind, but ultimately it is just a matter of desugaring the syntax. There's no semantic difference.Result<T, E>is more complicated due to theE::from(err)call, which can't be stableconst fnright now anyways.If it's possible to stabilize it on
Option<T>without changing the desugar, that would be great. But I don't think it is possible until we stabilize bothTryandconst_trait_impl, which seems a ways off still.Plus, who knows. There might be a small performance benefit to changing the desugaring to something simpler.
Jacob Pratt at 2022-03-01 06:40:44
The reason we can't do this is that during desugaring we don't know what the type is. We compute types after desugaring.
Oli Scherer at 2022-03-01 07:39:03
Ah, dang. For some reason I thought the order was the other way around.
Jacob Pratt at 2022-03-01 07:43:16