File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
compiler/rustc_passes/src
src/test/ui/rfc-2632-const-trait-impl Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -173,6 +173,12 @@ impl<'tcx> CheckConstVisitor<'tcx> {
173173 None => return true ,
174174 } ;
175175
176+ // If the function belongs to a trait, then it must enable the const_trait_impl
177+ // feature to use that trait function (with a const default body).
178+ if tcx. trait_of_item ( def_id) . is_some ( ) {
179+ return true ;
180+ }
181+
176182 // If this crate is not using stability attributes, or this function is not claiming to be a
177183 // stable `const fn`, that is all that is required.
178184 if !tcx. features ( ) . staged_api || tcx. has_attr ( def_id, sym:: rustc_const_unstable) {
Original file line number Diff line number Diff line change 1+ // check-pass
2+
3+ #![ feature( staged_api) ]
4+ #![ feature( const_trait_impl) ]
5+ #![ feature( const_fn_trait_bound) ]
6+ #![ feature( const_t_try) ]
7+ #![ feature( const_try) ]
8+ #![ feature( try_trait_v2) ]
9+
10+ #![ stable( feature = "foo" , since = "1.0" ) ]
11+
12+ use std:: ops:: { ControlFlow , FromResidual , Try } ;
13+
14+ #[ stable( feature = "foo" , since = "1.0" ) ]
15+ pub struct T ;
16+
17+ #[ stable( feature = "foo" , since = "1.0" ) ]
18+ #[ rustc_const_unstable( feature = "const_t_try" , issue = "none" ) ]
19+ impl const Try for T {
20+ type Output = T ;
21+ type Residual = T ;
22+
23+ fn from_output ( t : T ) -> T {
24+ t
25+ }
26+
27+ fn branch ( self ) -> ControlFlow < T , T > {
28+ ControlFlow :: Continue ( self )
29+ }
30+ }
31+
32+ #[ stable( feature = "foo" , since = "1.0" ) ]
33+ #[ rustc_const_unstable( feature = "const_t_try" , issue = "none" ) ]
34+ impl const FromResidual for T {
35+ fn from_residual ( t : T ) -> T {
36+ t
37+ }
38+ }
39+
40+ #[ stable( feature = "foo" , since = "1.0" ) ]
41+ pub trait Tr {
42+ #[ default_method_body_is_const]
43+ #[ stable( feature = "foo" , since = "1.0" ) ]
44+ fn bar ( ) -> T {
45+ T ?
46+ // Should be allowed.
47+ // Must enable unstable features to call this trait fn in const contexts.
48+ }
49+ }
50+
51+ fn main ( ) { }
You can’t perform that action at this time.
0 commit comments