@@ -466,32 +466,38 @@ impl MacResult for MacEager {
466
466
#[ derive( Copy , Clone ) ]
467
467
pub struct DummyResult {
468
468
expr_only : bool ,
469
- span : Span
469
+ is_error : bool ,
470
+ span : Span ,
470
471
}
471
472
472
473
impl DummyResult {
473
474
/// Create a default MacResult that can be anything.
474
475
///
475
476
/// Use this as a return value after hitting any errors and
476
477
/// calling `span_err`.
477
- pub fn any ( sp : Span ) -> Box < dyn MacResult +' static > {
478
- Box :: new ( DummyResult { expr_only : false , span : sp } )
478
+ pub fn any ( span : Span ) -> Box < dyn MacResult +' static > {
479
+ Box :: new ( DummyResult { expr_only : false , is_error : true , span } )
480
+ }
481
+
482
+ /// Same as `any`, but must be a valid fragment, not error.
483
+ pub fn any_valid ( span : Span ) -> Box < dyn MacResult +' static > {
484
+ Box :: new ( DummyResult { expr_only : false , is_error : false , span } )
479
485
}
480
486
481
487
/// Create a default MacResult that can only be an expression.
482
488
///
483
489
/// Use this for macros that must expand to an expression, so even
484
490
/// if an error is encountered internally, the user will receive
485
491
/// an error that they also used it in the wrong place.
486
- pub fn expr ( sp : Span ) -> Box < dyn MacResult +' static > {
487
- Box :: new ( DummyResult { expr_only : true , span : sp } )
492
+ pub fn expr ( span : Span ) -> Box < dyn MacResult +' static > {
493
+ Box :: new ( DummyResult { expr_only : true , is_error : true , span } )
488
494
}
489
495
490
496
/// A plain dummy expression.
491
- pub fn raw_expr ( sp : Span ) -> P < ast:: Expr > {
497
+ pub fn raw_expr ( sp : Span , is_error : bool ) -> P < ast:: Expr > {
492
498
P ( ast:: Expr {
493
499
id : ast:: DUMMY_NODE_ID ,
494
- node : ast:: ExprKind :: Err ,
500
+ node : if is_error { ast:: ExprKind :: Err } else { ast :: ExprKind :: Tup ( Vec :: new ( ) ) } ,
495
501
span : sp,
496
502
attrs : ThinVec :: new ( ) ,
497
503
} )
@@ -507,18 +513,18 @@ impl DummyResult {
507
513
}
508
514
509
515
/// A plain dummy type.
510
- pub fn raw_ty ( sp : Span ) -> P < ast:: Ty > {
516
+ pub fn raw_ty ( sp : Span , is_error : bool ) -> P < ast:: Ty > {
511
517
P ( ast:: Ty {
512
518
id : ast:: DUMMY_NODE_ID ,
513
- node : ast:: TyKind :: Err ,
519
+ node : if is_error { ast:: TyKind :: Err } else { ast :: TyKind :: Tup ( Vec :: new ( ) ) } ,
514
520
span : sp
515
521
} )
516
522
}
517
523
}
518
524
519
525
impl MacResult for DummyResult {
520
526
fn make_expr ( self : Box < DummyResult > ) -> Option < P < ast:: Expr > > {
521
- Some ( DummyResult :: raw_expr ( self . span ) )
527
+ Some ( DummyResult :: raw_expr ( self . span , self . is_error ) )
522
528
}
523
529
524
530
fn make_pat ( self : Box < DummyResult > ) -> Option < P < ast:: Pat > > {
@@ -561,13 +567,13 @@ impl MacResult for DummyResult {
561
567
fn make_stmts ( self : Box < DummyResult > ) -> Option < SmallVec < [ ast:: Stmt ; 1 ] > > {
562
568
Some ( smallvec ! [ ast:: Stmt {
563
569
id: ast:: DUMMY_NODE_ID ,
564
- node: ast:: StmtKind :: Expr ( DummyResult :: raw_expr( self . span) ) ,
570
+ node: ast:: StmtKind :: Expr ( DummyResult :: raw_expr( self . span, self . is_error ) ) ,
565
571
span: self . span,
566
572
} ] )
567
573
}
568
574
569
575
fn make_ty ( self : Box < DummyResult > ) -> Option < P < ast:: Ty > > {
570
- Some ( DummyResult :: raw_ty ( self . span ) )
576
+ Some ( DummyResult :: raw_ty ( self . span , self . is_error ) )
571
577
}
572
578
}
573
579
0 commit comments