Decision on "must define before use" for opaque types · Issue #117866 · rust-lang/rust (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
Nominating for T-lang to decide whether the following rule should hold for opaque types:
If the body of an item that may define the hidden type of some opaque does define that hidden type, it must do so syntactically before using the opaque type in a non-defining way.
This restriction is called "must define before use."
Consider:
use core::convert::identity;
struct I; struct IShow; impl I { fn show(&self) -> IShow { IShow } }
struct OnIShow; trait OnI { fn show(&self) -> OnIShow { OnIShow } } impl OnI for I {}
fn test(n: bool) -> impl OnI {
let true = n else { loop {} };
let x = test(!n); //~ NOTE this is the opaque type
let _: OnIShow = x.show(); //~ NOTE this is a non-defining use
//^ ERROR if the body registers a hidden type for the opaque, it
// must do so before using it opaquely
let _: IShow = identity::(x).show();
//^ NOTE this registers a hidden type for the opaque, but does so
// too late
loop {}
}
fn main() {}
Because the code above works today, this would be a breaking change for RPIT.