Skip to content

Commit 66dd5b7

Browse files
authored
Rollup merge of rust-lang#63528 - petrochenkov:anyany, r=estebank
syntax: Remove `DummyResult::expr_only` The effect is that if a built-in macro both returns an erroneous AST fragment and is used in unexpected position, then the incorrect position error won't be reported. This combination of two errors should be rare and isn't worth an extra field that makes people ask questions in comments. (There wasn't even a test making sure it worked.) Addresses rust-lang#63468 (comment) r? @estebank
2 parents ddeaf59 + 376636e commit 66dd5b7

File tree

8 files changed

+27
-54
lines changed

8 files changed

+27
-54
lines changed

src/libsyntax/ext/base.rs

+6-33
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ impl MacResult for MacEager {
405405
/// after hitting errors.
406406
#[derive(Copy, Clone)]
407407
pub struct DummyResult {
408-
expr_only: bool,
409408
is_error: bool,
410409
span: Span,
411410
}
@@ -416,21 +415,12 @@ impl DummyResult {
416415
/// Use this as a return value after hitting any errors and
417416
/// calling `span_err`.
418417
pub fn any(span: Span) -> Box<dyn MacResult+'static> {
419-
Box::new(DummyResult { expr_only: false, is_error: true, span })
418+
Box::new(DummyResult { is_error: true, span })
420419
}
421420

422421
/// Same as `any`, but must be a valid fragment, not error.
423422
pub fn any_valid(span: Span) -> Box<dyn MacResult+'static> {
424-
Box::new(DummyResult { expr_only: false, is_error: false, span })
425-
}
426-
427-
/// Creates a default MacResult that can only be an expression.
428-
///
429-
/// Use this for macros that must expand to an expression, so even
430-
/// if an error is encountered internally, the user will receive
431-
/// an error that they also used it in the wrong place.
432-
pub fn expr(span: Span) -> Box<dyn MacResult+'static> {
433-
Box::new(DummyResult { expr_only: true, is_error: true, span })
423+
Box::new(DummyResult { is_error: false, span })
434424
}
435425

436426
/// A plain dummy expression.
@@ -472,36 +462,19 @@ impl MacResult for DummyResult {
472462
}
473463

474464
fn make_items(self: Box<DummyResult>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
475-
// this code needs a comment... why not always just return the Some() ?
476-
if self.expr_only {
477-
None
478-
} else {
479-
Some(SmallVec::new())
480-
}
465+
Some(SmallVec::new())
481466
}
482467

483468
fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[ast::ImplItem; 1]>> {
484-
if self.expr_only {
485-
None
486-
} else {
487-
Some(SmallVec::new())
488-
}
469+
Some(SmallVec::new())
489470
}
490471

491472
fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[ast::TraitItem; 1]>> {
492-
if self.expr_only {
493-
None
494-
} else {
495-
Some(SmallVec::new())
496-
}
473+
Some(SmallVec::new())
497474
}
498475

499476
fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[ast::ForeignItem; 1]>> {
500-
if self.expr_only {
501-
None
502-
} else {
503-
Some(SmallVec::new())
504-
}
477+
Some(SmallVec::new())
505478
}
506479

507480
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {

src/libsyntax_ext/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
4747
-> Box<dyn base::MacResult + 'cx> {
4848
let mut inline_asm = match parse_inline_asm(cx, sp, tts) {
4949
Ok(Some(inline_asm)) => inline_asm,
50-
Ok(None) => return DummyResult::expr(sp),
50+
Ok(None) => return DummyResult::any(sp),
5151
Err(mut err) => {
5252
err.emit();
53-
return DummyResult::expr(sp);
53+
return DummyResult::any(sp);
5454
}
5555
};
5656

src/libsyntax_ext/assert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn expand_assert<'cx>(
2020
Ok(assert) => assert,
2121
Err(mut err) => {
2222
err.emit();
23-
return DummyResult::expr(sp);
23+
return DummyResult::any(sp);
2424
}
2525
};
2626

src/libsyntax_ext/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn expand_cfg(
2525
}
2626
Err(mut err) => {
2727
err.emit();
28-
DummyResult::expr(sp)
28+
DummyResult::any(sp)
2929
}
3030
}
3131
}

src/libsyntax_ext/concat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::ast;
2-
use syntax::ext::base;
2+
use syntax::ext::base::{self, DummyResult};
33
use syntax::symbol::Symbol;
44
use syntax::tokenstream;
55

@@ -12,7 +12,7 @@ pub fn expand_syntax_ext(
1212
) -> Box<dyn base::MacResult + 'static> {
1313
let es = match base::get_exprs_from_tts(cx, sp, tts) {
1414
Some(e) => e,
15-
None => return base::DummyResult::expr(sp),
15+
None => return DummyResult::any(sp),
1616
};
1717
let mut accumulator = String::new();
1818
let mut missing_literal = vec![];
@@ -55,9 +55,9 @@ pub fn expand_syntax_ext(
5555
let mut err = cx.struct_span_err(missing_literal, "expected a literal");
5656
err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
5757
err.emit();
58-
return base::DummyResult::expr(sp);
58+
return DummyResult::any(sp);
5959
} else if has_errors {
60-
return base::DummyResult::expr(sp);
60+
return DummyResult::any(sp);
6161
}
6262
let sp = sp.apply_mark(cx.current_expansion.id);
6363
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))

src/libsyntax_ext/env.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
1616
tts: &[tokenstream::TokenTree])
1717
-> Box<dyn base::MacResult + 'cx> {
1818
let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") {
19-
None => return DummyResult::expr(sp),
19+
None => return DummyResult::any(sp),
2020
Some(v) => v,
2121
};
2222

@@ -50,35 +50,35 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt<'_>,
5050
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
5151
Some(ref exprs) if exprs.is_empty() => {
5252
cx.span_err(sp, "env! takes 1 or 2 arguments");
53-
return DummyResult::expr(sp);
53+
return DummyResult::any(sp);
5454
}
55-
None => return DummyResult::expr(sp),
55+
None => return DummyResult::any(sp),
5656
Some(exprs) => exprs.into_iter(),
5757
};
5858

5959
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
60-
None => return DummyResult::expr(sp),
60+
None => return DummyResult::any(sp),
6161
Some((v, _style)) => v,
6262
};
6363
let msg = match exprs.next() {
6464
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
6565
Some(second) => {
6666
match expr_to_string(cx, second, "expected string literal") {
67-
None => return DummyResult::expr(sp),
67+
None => return DummyResult::any(sp),
6868
Some((s, _style)) => s,
6969
}
7070
}
7171
};
7272

7373
if exprs.next().is_some() {
7474
cx.span_err(sp, "env! takes 1 or 2 arguments");
75-
return DummyResult::expr(sp);
75+
return DummyResult::any(sp);
7676
}
7777

7878
let e = match env::var(&*var.as_str()) {
7979
Err(_) => {
8080
cx.span_err(sp, &msg.as_str());
81-
return DummyResult::expr(sp);
81+
return DummyResult::any(sp);
8282
}
8383
Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
8484
};

src/libsyntax_ext/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ fn expand_format_args_impl<'cx>(
805805
}
806806
Err(mut err) => {
807807
err.emit();
808-
DummyResult::expr(sp)
808+
DummyResult::any(sp)
809809
}
810810
}
811811
}

src/libsyntax_ext/source_util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
111111
-> Box<dyn base::MacResult+'static> {
112112
let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
113113
Some(f) => f,
114-
None => return DummyResult::expr(sp)
114+
None => return DummyResult::any(sp)
115115
};
116116
let file = cx.resolve_path(file, sp);
117117
match fs::read_to_string(&file) {
@@ -126,11 +126,11 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
126126
},
127127
Err(ref e) if e.kind() == ErrorKind::InvalidData => {
128128
cx.span_err(sp, &format!("{} wasn't a utf-8 file", file.display()));
129-
DummyResult::expr(sp)
129+
DummyResult::any(sp)
130130
}
131131
Err(e) => {
132132
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
133-
DummyResult::expr(sp)
133+
DummyResult::any(sp)
134134
}
135135
}
136136
}
@@ -139,7 +139,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
139139
-> Box<dyn base::MacResult+'static> {
140140
let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
141141
Some(f) => f,
142-
None => return DummyResult::expr(sp)
142+
None => return DummyResult::any(sp)
143143
};
144144
let file = cx.resolve_path(file, sp);
145145
match fs::read(&file) {
@@ -158,7 +158,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
158158
},
159159
Err(e) => {
160160
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
161-
DummyResult::expr(sp)
161+
DummyResult::any(sp)
162162
}
163163
}
164164
}

0 commit comments

Comments
 (0)