File tree 7 files changed +66
-28
lines changed
src/tools/clippy/clippy_utils/src
7 files changed +66
-28
lines changed Original file line number Diff line number Diff line change @@ -2602,12 +2602,12 @@ impl CoroutineKind {
2602
2602
}
2603
2603
}
2604
2604
2605
- pub fn is_async ( self ) -> bool {
2606
- matches ! ( self , CoroutineKind :: Async { .. } )
2607
- }
2608
-
2609
- pub fn is_gen ( self ) -> bool {
2610
- matches ! ( self , CoroutineKind :: Gen { .. } )
2605
+ pub fn as_str ( self ) -> & ' static str {
2606
+ match self {
2607
+ CoroutineKind :: Async { .. } => "async" ,
2608
+ CoroutineKind :: Gen { .. } => "gen" ,
2609
+ CoroutineKind :: AsyncGen { .. } => "async gen" ,
2610
+ }
2611
2611
}
2612
2612
2613
2613
pub fn closure_id ( self ) -> NodeId {
Original file line number Diff line number Diff line change @@ -40,15 +40,15 @@ ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
40
40
41
41
ast_passes_bound_in_context = bounds on `type`s in { $ctx } have no effect
42
42
43
- ast_passes_const_and_async = functions cannot be both `const` and `async`
44
- .const = `const` because of this
45
- .async = `async` because of this
46
- .label = { " " }
47
-
48
43
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
49
44
.const = `const` because of this
50
45
.variadic = C-variadic because of this
51
46
47
+ ast_passes_const_and_coroutine = functions cannot be both `const` and `{ $coroutine_kind } `
48
+ .const = `const` because of this
49
+ .coroutine = `{ $coroutine_kind } ` because of this
50
+ .label = { " " }
51
+
52
52
ast_passes_const_bound_trait_object = const trait bounds are not allowed in trait object types
53
53
54
54
ast_passes_const_without_body =
Original file line number Diff line number Diff line change @@ -1418,21 +1418,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1418
1418
1419
1419
// Functions cannot both be `const async` or `const gen`
1420
1420
if let Some ( & FnHeader {
1421
- constness : Const :: Yes ( cspan ) ,
1421
+ constness : Const :: Yes ( const_span ) ,
1422
1422
coroutine_kind : Some ( coroutine_kind) ,
1423
1423
..
1424
1424
} ) = fk. header ( )
1425
1425
{
1426
- let aspan = match coroutine_kind {
1427
- CoroutineKind :: Async { span : aspan, .. }
1428
- | CoroutineKind :: Gen { span : aspan, .. }
1429
- | CoroutineKind :: AsyncGen { span : aspan, .. } => aspan,
1430
- } ;
1431
- // FIXME(gen_blocks): Report a different error for `const gen`
1432
- self . dcx ( ) . emit_err ( errors:: ConstAndAsync {
1433
- spans : vec ! [ cspan, aspan] ,
1434
- cspan,
1435
- aspan,
1426
+ self . dcx ( ) . emit_err ( errors:: ConstAndCoroutine {
1427
+ spans : vec ! [ coroutine_kind. span( ) , const_span] ,
1428
+ const_span,
1429
+ coroutine_span : coroutine_kind. span ( ) ,
1430
+ coroutine_kind : coroutine_kind. as_str ( ) ,
1436
1431
span,
1437
1432
} ) ;
1438
1433
}
Original file line number Diff line number Diff line change @@ -657,16 +657,17 @@ pub(crate) enum TildeConstReason {
657
657
}
658
658
659
659
#[ derive( Diagnostic ) ]
660
- #[ diag( ast_passes_const_and_async ) ]
661
- pub ( crate ) struct ConstAndAsync {
660
+ #[ diag( ast_passes_const_and_coroutine ) ]
661
+ pub ( crate ) struct ConstAndCoroutine {
662
662
#[ primary_span]
663
663
pub spans : Vec < Span > ,
664
664
#[ label( ast_passes_const) ]
665
- pub cspan : Span ,
666
- #[ label( ast_passes_async ) ]
667
- pub aspan : Span ,
665
+ pub const_span : Span ,
666
+ #[ label( ast_passes_coroutine ) ]
667
+ pub coroutine_span : Span ,
668
668
#[ label]
669
669
pub span : Span ,
670
+ pub coroutine_kind : & ' static str ,
670
671
}
671
672
672
673
#[ derive( Diagnostic ) ]
Original file line number Diff line number Diff line change @@ -221,7 +221,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
221
221
) => {
222
222
eq_closure_binder ( lb, rb)
223
223
&& lc == rc
224
- && la . map_or ( false , CoroutineKind :: is_async ) == ra . map_or ( false , CoroutineKind :: is_async )
224
+ && eq_coroutine_kind ( * la , * ra )
225
225
&& lm == rm
226
226
&& eq_fn_decl ( lf, rf)
227
227
&& eq_expr ( le, re)
@@ -241,6 +241,16 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
241
241
}
242
242
}
243
243
244
+ fn eq_coroutine_kind ( a : Option < CoroutineKind > , b : Option < CoroutineKind > ) -> bool {
245
+ match ( a, b) {
246
+ ( Some ( CoroutineKind :: Async { .. } ) , Some ( CoroutineKind :: Async { .. } ) )
247
+ | ( Some ( CoroutineKind :: Gen { .. } ) , Some ( CoroutineKind :: Gen { .. } ) )
248
+ | ( Some ( CoroutineKind :: AsyncGen { .. } ) , Some ( CoroutineKind :: AsyncGen { .. } ) )
249
+ | ( None , None ) => true ,
250
+ _ => false ,
251
+ }
252
+ }
253
+
244
254
pub fn eq_field ( l : & ExprField , r : & ExprField ) -> bool {
245
255
l. is_placeholder == r. is_placeholder
246
256
&& eq_id ( l. ident , r. ident )
Original file line number Diff line number Diff line change
1
+ //@ edition:2024
2
+ //@ compile-flags: -Zunstable-options
3
+
4
+ #![ feature( gen_blocks) ]
5
+
6
+ const gen fn a ( ) { }
7
+ //~^ ERROR functions cannot be both `const` and `gen`
8
+
9
+ const async gen fn b ( ) { }
10
+ //~^ ERROR functions cannot be both `const` and `async gen`
11
+
12
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: functions cannot be both `const` and `gen`
2
+ --> $DIR/const_gen_fn.rs:6:1
3
+ |
4
+ LL | const gen fn a() {}
5
+ | ^^^^^-^^^----------
6
+ | | |
7
+ | | `gen` because of this
8
+ | `const` because of this
9
+
10
+ error: functions cannot be both `const` and `async gen`
11
+ --> $DIR/const_gen_fn.rs:9:1
12
+ |
13
+ LL | const async gen fn b() {}
14
+ | ^^^^^-^^^^^^^^^----------
15
+ | | |
16
+ | | `async gen` because of this
17
+ | `const` because of this
18
+
19
+ error: aborting due to 2 previous errors
20
+
You can’t perform that action at this time.
0 commit comments