Skip to content

Commit a053ae2

Browse files
committed
Fix trace_macros and log_syntax
1 parent 577427e commit a053ae2

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

src/libsyntax/ext/base.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -466,32 +466,38 @@ impl MacResult for MacEager {
466466
#[derive(Copy, Clone)]
467467
pub struct DummyResult {
468468
expr_only: bool,
469-
span: Span
469+
is_error: bool,
470+
span: Span,
470471
}
471472

472473
impl DummyResult {
473474
/// Create a default MacResult that can be anything.
474475
///
475476
/// Use this as a return value after hitting any errors and
476477
/// 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 })
479485
}
480486

481487
/// Create a default MacResult that can only be an expression.
482488
///
483489
/// Use this for macros that must expand to an expression, so even
484490
/// if an error is encountered internally, the user will receive
485491
/// 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 })
488494
}
489495

490496
/// 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> {
492498
P(ast::Expr {
493499
id: ast::DUMMY_NODE_ID,
494-
node: ast::ExprKind::Err,
500+
node: if is_error { ast::ExprKind::Err } else { ast::ExprKind::Tup(Vec::new()) },
495501
span: sp,
496502
attrs: ThinVec::new(),
497503
})
@@ -507,18 +513,18 @@ impl DummyResult {
507513
}
508514

509515
/// 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> {
511517
P(ast::Ty {
512518
id: ast::DUMMY_NODE_ID,
513-
node: ast::TyKind::Err,
519+
node: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) },
514520
span: sp
515521
})
516522
}
517523
}
518524

519525
impl MacResult for DummyResult {
520526
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))
522528
}
523529

524530
fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> {
@@ -561,13 +567,13 @@ impl MacResult for DummyResult {
561567
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {
562568
Some(smallvec![ast::Stmt {
563569
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)),
565571
span: self.span,
566572
}])
567573
}
568574

569575
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))
571577
}
572578
}
573579

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4984,7 +4984,7 @@ impl<'a> Parser<'a> {
49844984
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
49854985
Some(Stmt {
49864986
id: ast::DUMMY_NODE_ID,
4987-
node: StmtKind::Expr(DummyResult::raw_expr(self.span)),
4987+
node: StmtKind::Expr(DummyResult::raw_expr(self.span, true)),
49884988
span: self.span,
49894989
})
49904990
}

src/libsyntax_ext/deriving/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
7979
span_err!(cx, trait_span, E0665,
8080
"`Default` cannot be derived for enums, only structs");
8181
// let compilation continue
82-
DummyResult::raw_expr(trait_span)
82+
DummyResult::raw_expr(trait_span, true)
8383
}
8484
_ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"),
8585
};

src/libsyntax_ext/format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ impl<'a, 'b> Context<'a, 'b> {
676676
"X" => "UpperHex",
677677
_ => {
678678
ecx.span_err(sp, &format!("unknown format trait `{}`", *tyname));
679-
return DummyResult::raw_expr(sp);
679+
return DummyResult::raw_expr(sp, true);
680680
}
681681
}
682682
}
@@ -771,7 +771,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
771771
Applicability::MaybeIncorrect,
772772
);
773773
err.emit();
774-
return DummyResult::raw_expr(sp);
774+
return DummyResult::raw_expr(sp, true);
775775
}
776776
};
777777

@@ -806,7 +806,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
806806
e.note(&note);
807807
}
808808
e.emit();
809-
return DummyResult::raw_expr(sp);
809+
return DummyResult::raw_expr(sp, true);
810810
}
811811

812812
let arg_spans = parser.arg_places.iter()

src/libsyntax_ext/log_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut base::ExtCtxt,
3030
println!("{}", print::pprust::tts_to_string(tts));
3131

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

src/libsyntax_ext/trace_macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt,
3838
_ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"),
3939
}
4040

41-
base::DummyResult::any(sp)
41+
base::DummyResult::any_valid(sp)
4242
}

0 commit comments

Comments
 (0)