Skip to content

Commit b99fb2f

Browse files
committed
Fix trace_macros and log_syntax
1 parent 4ac5925 commit b99fb2f

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

src/libsyntax/ext/base.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,32 +456,38 @@ impl MacResult for MacEager {
456456
#[derive(Copy, Clone)]
457457
pub struct DummyResult {
458458
expr_only: bool,
459-
span: Span
459+
is_error: bool,
460+
span: Span,
460461
}
461462

462463
impl DummyResult {
463464
/// Create a default MacResult that can be anything.
464465
///
465466
/// Use this as a return value after hitting any errors and
466467
/// calling `span_err`.
467-
pub fn any(sp: Span) -> Box<dyn MacResult+'static> {
468-
Box::new(DummyResult { expr_only: false, span: sp })
468+
pub fn any(span: Span) -> Box<dyn MacResult+'static> {
469+
Box::new(DummyResult { expr_only: false, is_error: true, span })
470+
}
471+
472+
/// Same as `any`, but must be a valid fragment, not error.
473+
pub fn any_valid(span: Span) -> Box<dyn MacResult+'static> {
474+
Box::new(DummyResult { expr_only: false, is_error: false, span })
469475
}
470476

471477
/// Create a default MacResult that can only be an expression.
472478
///
473479
/// Use this for macros that must expand to an expression, so even
474480
/// if an error is encountered internally, the user will receive
475481
/// an error that they also used it in the wrong place.
476-
pub fn expr(sp: Span) -> Box<dyn MacResult+'static> {
477-
Box::new(DummyResult { expr_only: true, span: sp })
482+
pub fn expr(span: Span) -> Box<dyn MacResult+'static> {
483+
Box::new(DummyResult { expr_only: true, is_error: true, span })
478484
}
479485

480486
/// A plain dummy expression.
481-
pub fn raw_expr(sp: Span) -> P<ast::Expr> {
487+
pub fn raw_expr(sp: Span, is_error: bool) -> P<ast::Expr> {
482488
P(ast::Expr {
483489
id: ast::DUMMY_NODE_ID,
484-
node: ast::ExprKind::Err,
490+
node: if is_error { ast::ExprKind::Err } else { ast::ExprKind::Tup(Vec::new()) },
485491
span: sp,
486492
attrs: ThinVec::new(),
487493
})
@@ -497,18 +503,18 @@ impl DummyResult {
497503
}
498504

499505
/// A plain dummy type.
500-
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
506+
pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> {
501507
P(ast::Ty {
502508
id: ast::DUMMY_NODE_ID,
503-
node: ast::TyKind::Err,
509+
node: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) },
504510
span: sp
505511
})
506512
}
507513
}
508514

509515
impl MacResult for DummyResult {
510516
fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> {
511-
Some(DummyResult::raw_expr(self.span))
517+
Some(DummyResult::raw_expr(self.span, self.is_error))
512518
}
513519

514520
fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
@@ -551,13 +557,13 @@ impl MacResult for DummyResult {
551557
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {
552558
Some(smallvec![ast::Stmt {
553559
id: ast::DUMMY_NODE_ID,
554-
node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span)),
560+
node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.is_error)),
555561
span: self.span,
556562
}])
557563
}
558564

559565
fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
560-
Some(DummyResult::raw_ty(self.span))
566+
Some(DummyResult::raw_ty(self.span, self.is_error))
561567
}
562568
}
563569

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4974,7 +4974,7 @@ impl<'a> Parser<'a> {
49744974
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
49754975
Some(Stmt {
49764976
id: ast::DUMMY_NODE_ID,
4977-
node: StmtKind::Expr(DummyResult::raw_expr(self.span)),
4977+
node: StmtKind::Expr(DummyResult::raw_expr(self.span, true)),
49784978
span: self.span,
49794979
})
49804980
}

src/libsyntax_ext/deriving/default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
6969
span_err!(cx, trait_span, E0665,
7070
"`Default` cannot be derived for enums, only structs");
7171
// let compilation continue
72-
DummyResult::raw_expr(trait_span)
72+
DummyResult::raw_expr(trait_span, true)
7373
}
7474
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
7575
};

src/libsyntax_ext/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ impl<'a, 'b> Context<'a, 'b> {
666666
"X" => "UpperHex",
667667
_ => {
668668
ecx.span_err(sp, &format!("unknown format trait `{}`", *tyname));
669-
return DummyResult::raw_expr(sp);
669+
return DummyResult::raw_expr(sp, true);
670670
}
671671
}
672672
}
@@ -761,7 +761,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
761761
Applicability::MaybeIncorrect,
762762
);
763763
err.emit();
764-
return DummyResult::raw_expr(sp);
764+
return DummyResult::raw_expr(sp, true);
765765
}
766766
};
767767

@@ -857,7 +857,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
857857
e.span_label(sp, label);
858858
}
859859
e.emit();
860-
return DummyResult::raw_expr(sp);
860+
return DummyResult::raw_expr(sp, true);
861861
}
862862

863863
let arg_spans = parser.arg_places.iter()

src/libsyntax_ext/log_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut base::ExtCtxt,
2020
println!("{}", print::pprust::tts_to_string(tts));
2121

2222
// any so that `log_syntax` can be invoked as an expression and item.
23-
base::DummyResult::any(sp)
23+
base::DummyResult::any_valid(sp)
2424
}

src/libsyntax_ext/trace_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
2828
_ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"),
2929
}
3030

31-
base::DummyResult::any(sp)
31+
base::DummyResult::any_valid(sp)
3232
}

0 commit comments

Comments
 (0)