Skip to content

Remove struct_span_err function from ExtCtxt #76518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 44 additions & 32 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ fn parse_args<'a>(
let mut p = ecx.new_parser_from_tts(tts);

if p.token == token::Eof {
return Err(ecx.struct_span_err(sp, "requires at least a template string argument"));
return Err(ecx.sess.struct_span_err(sp, "requires at least a template string argument"));
}

// Detect use of the legacy llvm_asm! syntax (which used to be called asm!)
if p.look_ahead(1, |t| *t == token::Colon || *t == token::ModSep) {
let mut err =
ecx.struct_span_err(sp, "the legacy LLVM-style asm! syntax is no longer supported");
let mut err = ecx
.sess
.struct_span_err(sp, "the legacy LLVM-style asm! syntax is no longer supported");
err.note("consider migrating to the new asm! syntax specified in RFC 2873");
err.note("alternatively, switch to llvm_asm! to keep your code working as it is");

Expand Down Expand Up @@ -67,7 +68,7 @@ fn parse_args<'a>(
if !p.eat(&token::Comma) {
if allow_templates {
// After a template string, we always expect *only* a comma...
let mut err = ecx.struct_span_err(p.token.span, "expected token: `,`");
let mut err = ecx.sess.struct_span_err(p.token.span, "expected token: `,`");
err.span_label(p.token.span, "expected `,`");
p.maybe_annotate_with_ascription(&mut err, false);
return Err(err);
Expand Down Expand Up @@ -142,6 +143,7 @@ fn parse_args<'a>(
ast::ExprKind::Path(..) => {}
_ => {
let err = ecx
.sess
.struct_span_err(expr.span, "argument to `sym` must be a path expression");
return Err(err);
}
Expand All @@ -156,7 +158,7 @@ fn parse_args<'a>(
ast::ExprKind::MacCall(..) => {}
_ => {
let errstr = "expected operand, options, or additional template string";
let mut err = ecx.struct_span_err(template.span, errstr);
let mut err = ecx.sess.struct_span_err(template.span, errstr);
err.span_label(template.span, errstr);
return Err(err);
}
Expand All @@ -175,26 +177,30 @@ fn parse_args<'a>(
// Validate the order of named, positional & explicit register operands and options. We do
// this at the end once we have the full span of the argument available.
if !args.options_spans.is_empty() {
ecx.struct_span_err(span, "arguments are not allowed after options")
ecx.sess
.struct_span_err(span, "arguments are not allowed after options")
.span_labels(args.options_spans.clone(), "previous options")
.span_label(span, "argument")
.emit();
}
if explicit_reg {
if name.is_some() {
ecx.struct_span_err(span, "explicit register arguments cannot have names").emit();
ecx.sess
.struct_span_err(span, "explicit register arguments cannot have names")
.emit();
}
args.reg_args.insert(slot);
} else if let Some(name) = name {
if let Some(&prev) = args.named_args.get(&name) {
ecx.struct_span_err(span, &format!("duplicate argument named `{}`", name))
ecx.sess
.struct_span_err(span, &format!("duplicate argument named `{}`", name))
.span_label(args.operands[prev].1, "previously here")
.span_label(span, "duplicate argument")
.emit();
continue;
}
if !args.reg_args.is_empty() {
let mut err = ecx.struct_span_err(
let mut err = ecx.sess.struct_span_err(
span,
"named arguments cannot follow explicit register arguments",
);
Expand All @@ -207,7 +213,7 @@ fn parse_args<'a>(
args.named_args.insert(name, slot);
} else {
if !args.named_args.is_empty() || !args.reg_args.is_empty() {
let mut err = ecx.struct_span_err(
let mut err = ecx.sess.struct_span_err(
span,
"positional arguments cannot follow named arguments \
or explicit register arguments",
Expand All @@ -228,25 +234,28 @@ fn parse_args<'a>(
&& args.options.contains(ast::InlineAsmOptions::READONLY)
{
let spans = args.options_spans.clone();
ecx.struct_span_err(spans, "the `nomem` and `readonly` options are mutually exclusive")
ecx.sess
.struct_span_err(spans, "the `nomem` and `readonly` options are mutually exclusive")
.emit();
}
if args.options.contains(ast::InlineAsmOptions::PURE)
&& args.options.contains(ast::InlineAsmOptions::NORETURN)
{
let spans = args.options_spans.clone();
ecx.struct_span_err(spans, "the `pure` and `noreturn` options are mutually exclusive")
ecx.sess
.struct_span_err(spans, "the `pure` and `noreturn` options are mutually exclusive")
.emit();
}
if args.options.contains(ast::InlineAsmOptions::PURE)
&& !args.options.intersects(ast::InlineAsmOptions::NOMEM | ast::InlineAsmOptions::READONLY)
{
let spans = args.options_spans.clone();
ecx.struct_span_err(
spans,
"the `pure` option must be combined with either `nomem` or `readonly`",
)
.emit();
ecx.sess
.struct_span_err(
spans,
"the `pure` option must be combined with either `nomem` or `readonly`",
)
.emit();
}

let mut have_real_output = false;
Expand All @@ -266,14 +275,16 @@ fn parse_args<'a>(
}
}
if args.options.contains(ast::InlineAsmOptions::PURE) && !have_real_output {
ecx.struct_span_err(
args.options_spans.clone(),
"asm with `pure` option must have at least one output",
)
.emit();
ecx.sess
.struct_span_err(
args.options_spans.clone(),
"asm with `pure` option must have at least one output",
)
.emit();
}
if args.options.contains(ast::InlineAsmOptions::NORETURN) && !outputs_sp.is_empty() {
let err = ecx
.sess
.struct_span_err(outputs_sp, "asm outputs are not allowed with the `noreturn` option");

// Bail out now since this is likely to confuse MIR
Expand Down Expand Up @@ -445,7 +456,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
let err = parser.errors.remove(0);
let err_sp = template_span.from_inner(err.span);
let msg = &format!("invalid asm template string: {}", err.description);
let mut e = ecx.struct_span_err(err_sp, msg);
let mut e = ecx.sess.struct_span_err(err_sp, msg);
e.span_label(err_sp, err.label + " in asm template string");
if let Some(note) = err.note {
e.note(&note);
Expand Down Expand Up @@ -476,7 +487,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
|| args.reg_args.contains(&idx)
{
let msg = format!("invalid reference to argument at index {}", idx);
let mut err = ecx.struct_span_err(span, &msg);
let mut err = ecx.sess.struct_span_err(span, &msg);
err.span_label(span, "from here");

let positional_args = args.operands.len()
Expand Down Expand Up @@ -520,7 +531,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
Some(&idx) => Some(idx),
None => {
let msg = format!("there is no argument named `{}`", name);
ecx.struct_span_err(span, &msg[..]).emit();
ecx.sess.struct_span_err(span, &msg[..]).emit();
None
}
},
Expand All @@ -534,11 +545,12 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
.ty_span
.map(|sp| template_sp.from_inner(sp))
.unwrap_or(template_sp);
ecx.struct_span_err(
span,
"asm template modifier must be a single character",
)
.emit();
ecx.sess
.struct_span_err(
span,
"asm template modifier must be a single character",
)
.emit();
modifier = None;
}

Expand Down Expand Up @@ -580,7 +592,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
0 => {}
1 => {
let (sp, msg) = unused_operands.into_iter().next().unwrap();
let mut err = ecx.struct_span_err(sp, msg);
let mut err = ecx.sess.struct_span_err(sp, msg);
err.span_label(sp, msg);
err.help(&format!(
"if this argument is intentionally unused, \
Expand All @@ -590,7 +602,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
err.emit();
}
_ => {
let mut err = ecx.struct_span_err(
let mut err = ecx.sess.struct_span_err(
unused_operands.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
"multiple unused asm arguments",
);
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ fn parse_assert<'a>(
let mut parser = cx.new_parser_from_tts(stream);

if parser.token == token::Eof {
let mut err = cx.struct_span_err(sp, "macro requires a boolean expression as an argument");
let mut err =
cx.sess.struct_span_err(sp, "macro requires a boolean expression as an argument");
err.span_label(sp, "boolean expression required");
return Err(err);
}
Expand All @@ -82,7 +83,7 @@ fn parse_assert<'a>(
//
// Emit an error about semicolon and suggest removing it.
if parser.token == token::Semi {
let mut err = cx.struct_span_err(sp, "macro requires an expression as an argument");
let mut err = cx.sess.struct_span_err(sp, "macro requires an expression as an argument");
err.span_suggestion(
parser.token.span,
"try removing semicolon",
Expand All @@ -102,7 +103,7 @@ fn parse_assert<'a>(
// Emit an error and suggest inserting a comma.
let custom_message =
if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind {
let mut err = cx.struct_span_err(parser.token.span, "unexpected string literal");
let mut err = cx.sess.struct_span_err(parser.token.span, "unexpected string literal");
let comma_span = parser.prev_token.span.shrink_to_hi();
err.span_suggestion_short(
comma_span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn parse_cfg<'a>(
let mut p = cx.new_parser_from_tts(tts);

if p.token == token::Eof {
let mut err = cx.struct_span_err(sp, "macro requires a cfg-pattern as an argument");
let mut err = cx.sess.struct_span_err(sp, "macro requires a cfg-pattern as an argument");
err.span_label(sp, "cfg-pattern required");
return Err(err);
}
Expand All @@ -47,7 +47,7 @@ fn parse_cfg<'a>(
let _ = p.eat(&token::Comma);

if !p.eat(&token::Eof) {
return Err(cx.struct_span_err(sp, "expected 1 cfg-pattern"));
return Err(cx.sess.struct_span_err(sp, "expected 1 cfg-pattern"));
}

Ok(cfg)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn expand_concat(
}
}
if !missing_literal.is_empty() {
let mut err = cx.struct_span_err(missing_literal, "expected a literal");
let mut err = cx.sess.struct_span_err(missing_literal, "expected a literal");
err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
err.emit();
return DummyResult::any(sp);
Expand Down
22 changes: 12 additions & 10 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn parse_args<'a>(
let mut p = ecx.new_parser_from_tts(tts);

if p.token == token::Eof {
return Err(ecx.struct_span_err(sp, "requires at least a format string argument"));
return Err(ecx.sess.struct_span_err(sp, "requires at least a format string argument"));
}

let first_token = &p.token;
Expand Down Expand Up @@ -194,7 +194,8 @@ fn parse_args<'a>(
p.expect(&token::Eq)?;
let e = p.parse_expr()?;
if let Some(prev) = names.get(&ident.name) {
ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", ident))
ecx.sess
.struct_span_err(e.span, &format!("duplicate argument named `{}`", ident))
.span_label(args[*prev].span, "previously here")
.span_label(e.span, "duplicate argument")
.emit();
Expand All @@ -212,7 +213,7 @@ fn parse_args<'a>(
_ => {
let e = p.parse_expr()?;
if named {
let mut err = ecx.struct_span_err(
let mut err = ecx.sess.struct_span_err(
e.span,
"positional arguments cannot follow named arguments",
);
Expand Down Expand Up @@ -283,7 +284,7 @@ impl<'a, 'b> Context<'a, 'b> {
_ => {
let fmtsp = self.fmtsp;
let sp = arg.format.ty_span.map(|sp| fmtsp.from_inner(sp));
let mut err = self.ecx.struct_span_err(
let mut err = self.ecx.sess.struct_span_err(
sp.unwrap_or(fmtsp),
&format!("unknown format trait `{}`", arg.format.ty),
);
Expand Down Expand Up @@ -371,7 +372,7 @@ impl<'a, 'b> Context<'a, 'b> {
let count = self.pieces.len()
+ self.arg_with_formatting.iter().filter(|fmt| fmt.precision_span.is_some()).count();
if self.names.is_empty() && !numbered_position_args && count != self.args.len() {
e = self.ecx.struct_span_err(
e = self.ecx.sess.struct_span_err(
sp,
&format!(
"{} positional argument{} in format string, but {}",
Expand Down Expand Up @@ -403,7 +404,7 @@ impl<'a, 'b> Context<'a, 'b> {
format!("arguments {head} and {tail}", head = refs.join(", "), tail = reg)
};

e = self.ecx.struct_span_err(
e = self.ecx.sess.struct_span_err(
sp,
&format!(
"invalid reference to positional {} ({})",
Expand Down Expand Up @@ -555,7 +556,7 @@ impl<'a, 'b> Context<'a, 'b> {
} else {
self.fmtsp
};
let mut err = self.ecx.struct_span_err(sp, &msg[..]);
let mut err = self.ecx.sess.struct_span_err(sp, &msg[..]);

if capture_feature_enabled && !self.is_literal {
err.note(&format!(
Expand Down Expand Up @@ -987,7 +988,8 @@ pub fn expand_preparsed_format_args(
if !parser.errors.is_empty() {
let err = parser.errors.remove(0);
let sp = fmt_span.from_inner(err.span);
let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}", err.description));
let mut e =
ecx.sess.struct_span_err(sp, &format!("invalid format string: {}", err.description));
e.span_label(sp, err.label + " in format string");
if let Some(note) = err.note {
e.note(&note);
Expand Down Expand Up @@ -1093,11 +1095,11 @@ pub fn expand_preparsed_format_args(

let mut diag = {
if let [(sp, msg)] = &errs[..] {
let mut diag = cx.ecx.struct_span_err(*sp, *msg);
let mut diag = cx.ecx.sess.struct_span_err(*sp, *msg);
diag.span_label(*sp, *msg);
diag
} else {
let mut diag = cx.ecx.struct_span_err(
let mut diag = cx.ecx.sess.struct_span_err(
errs.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
"multiple unused formatting arguments",
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/global_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn parse_global_asm<'a>(
let mut p = cx.new_parser_from_tts(tts);

if p.token == token::Eof {
let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
let mut err = cx.sess.struct_span_err(sp, "macro requires a string literal as an argument");
err.span_label(sp, "string literal required");
return Err(err);
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_builtin_macros/src/llvm_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ fn parse_inline_asm<'a>(
let mut p2 = cx.new_parser_from_tts(tts.trees().take(first_colon).collect());

if p2.token == token::Eof {
let mut err =
cx.struct_span_err(sp, "macro requires a string literal as an argument");
let mut err = cx
.sess
.struct_span_err(sp, "macro requires a string literal as an argument");
err.span_label(sp, "string literal required");
return Err(err);
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ pub fn expand_test_or_bench(
let item = match item {
Annotatable::Item(i) => i,
other => {
cx.struct_span_err(
other.span(),
"`#[test]` attribute is only allowed on non associated functions",
)
.emit();
cx.sess
.struct_span_err(
other.span(),
"`#[test]` attribute is only allowed on non associated functions",
)
.emit();
return vec![other];
}
};
Expand Down
Loading