Rename consuming chaining methods on DiagnosticBuilder.

In #119606 I added them and used a `_mv` suffix, but that wasn't great.

A `with_` prefix has three different existing uses.
- Constructors, e.g. `Vec::with_capacity`.
- Wrappers that provide an environment to execute some code, e.g.
  `with_session_globals`.
- Consuming chaining methods, e.g. `Span::with_{lo,hi,ctxt}`.

The third case is exactly what we want, so this commit changes
`DiagnosticBuilder::foo_mv` to `DiagnosticBuilder::with_foo`.

Thanks to @compiler-errors for the suggestion.
This commit is contained in:
Nicholas Nethercote 2024-01-09 09:08:49 +11:00
parent 2ea7a37e11
commit ed76b0b882
76 changed files with 296 additions and 293 deletions

View file

@ -250,7 +250,7 @@ impl<'a> StringReader<'a> {
if starts_with_number {
let span = self.mk_sp(start, self.pos);
self.dcx().struct_err("lifetimes cannot start with a number")
.span_mv(span)
.with_span(span)
.stash(span, StashKey::LifetimeIsChar);
}
let ident = Symbol::intern(lifetime_name);
@ -397,7 +397,7 @@ impl<'a> StringReader<'a> {
if !terminated {
self.dcx()
.struct_span_fatal(self.mk_sp(start, end), "unterminated character literal")
.code_mv(error_code!(E0762))
.with_code(error_code!(E0762))
.emit()
}
self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' '
@ -409,7 +409,7 @@ impl<'a> StringReader<'a> {
self.mk_sp(start + BytePos(1), end),
"unterminated byte constant",
)
.code_mv(error_code!(E0763))
.with_code(error_code!(E0763))
.emit()
}
self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
@ -421,7 +421,7 @@ impl<'a> StringReader<'a> {
self.mk_sp(start, end),
"unterminated double quote string",
)
.code_mv(error_code!(E0765))
.with_code(error_code!(E0765))
.emit()
}
self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " "
@ -433,7 +433,7 @@ impl<'a> StringReader<'a> {
self.mk_sp(start + BytePos(1), end),
"unterminated double quote byte string",
)
.code_mv(error_code!(E0766))
.with_code(error_code!(E0766))
.emit()
}
self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
@ -445,7 +445,7 @@ impl<'a> StringReader<'a> {
self.mk_sp(start + BytePos(1), end),
"unterminated C string",
)
.code_mv(error_code!(E0767))
.with_code(error_code!(E0767))
.emit()
}
self.cook_c_string(token::CStr, Mode::CStr, start, end, 2, 1) // c" "

View file

@ -246,8 +246,8 @@ pub fn parse_cfg_attr(
match parse_in(parse_sess, tokens.clone(), "`cfg_attr` input", |p| p.parse_cfg_attr()) {
Ok(r) => return Some(r),
Err(e) => {
e.help_mv(format!("the valid syntax is `{CFG_ATTR_GRAMMAR_HELP}`"))
.note_mv(CFG_ATTR_NOTE_REF)
e.with_help(format!("the valid syntax is `{CFG_ATTR_GRAMMAR_HELP}`"))
.with_note(CFG_ATTR_NOTE_REF)
.emit();
}
}

View file

@ -204,8 +204,11 @@ impl<'a> Parser<'a> {
attr_sp,
fluent::parse_inner_attr_not_permitted_after_outer_doc_comment,
)
.span_label_mv(attr_sp, fluent::parse_label_attr)
.span_label_mv(prev_doc_comment_span, fluent::parse_label_prev_doc_comment)
.with_span_label(attr_sp, fluent::parse_label_attr)
.with_span_label(
prev_doc_comment_span,
fluent::parse_label_prev_doc_comment,
)
}
Some(InnerAttrForbiddenReason::AfterOuterAttribute { prev_outer_attr_sp }) => self
.dcx()
@ -213,8 +216,8 @@ impl<'a> Parser<'a> {
attr_sp,
fluent::parse_inner_attr_not_permitted_after_outer_attr,
)
.span_label_mv(attr_sp, fluent::parse_label_attr)
.span_label_mv(prev_outer_attr_sp, fluent::parse_label_prev_attr),
.with_span_label(attr_sp, fluent::parse_label_attr)
.with_span_label(prev_outer_attr_sp, fluent::parse_label_prev_attr),
Some(InnerAttrForbiddenReason::InCodeBlock) | None => {
self.dcx().struct_span_err(attr_sp, fluent::parse_inner_attr_not_permitted)
}

View file

@ -2847,8 +2847,8 @@ impl<'a> Parser<'a> {
let span = label.ident.span.to(self.prev_token.span);
self.dcx()
.struct_span_err(span, "block label not supported here")
.span_label_mv(span, "not supported here")
.tool_only_span_suggestion_mv(
.with_span_label(span, "not supported here")
.with_tool_only_span_suggestion(
label.ident.span.until(self.token.span),
"remove this block label",
"",

View file

@ -1753,7 +1753,7 @@ impl<'a> Parser<'a> {
err: impl FnOnce(&Self) -> DiagnosticBuilder<'a>,
) -> L {
if let Some(diag) = self.dcx().steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar) {
diag.span_suggestion_verbose_mv(
diag.with_span_suggestion_verbose(
lifetime.span.shrink_to_hi(),
"add `'` to close the char literal",
"'",
@ -1762,7 +1762,7 @@ impl<'a> Parser<'a> {
.emit();
} else {
err(self)
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
lifetime.span.shrink_to_hi(),
"add `'` to close the char literal",
"'",

View file

@ -145,7 +145,7 @@ impl<'a> Parser<'a> {
mistyped_const_ident.span,
format!("`const` keyword was mistyped as `{}`", mistyped_const_ident.as_str()),
)
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
mistyped_const_ident.span,
"use the `const` keyword",
kw::Const.as_str(),

View file

@ -741,11 +741,11 @@ impl<'a> Parser<'a> {
Ok(Some(item)) => items.extend(item),
Err(err) => {
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
err.span_label_mv(
err.with_span_label(
open_brace_span,
"while parsing this item list starting here",
)
.span_label_mv(self.prev_token.span, "the item list ends here")
.with_span_label(self.prev_token.span, "the item list ends here")
.emit();
break;
}
@ -765,8 +765,8 @@ impl<'a> Parser<'a> {
E0584,
"found a documentation comment that doesn't document anything",
)
.span_label_mv(self.token.span, "this doc comment doesn't document anything")
.help_mv(
.with_span_label(self.token.span, "this doc comment doesn't document anything")
.with_help(
"doc comments must come before what they document, if a comment was \
intended use `//`",
)
@ -1218,7 +1218,7 @@ impl<'a> Parser<'a> {
let before_trait = trai.path.span.shrink_to_lo();
let const_up_to_impl = const_span.with_hi(impl_span.lo());
err.multipart_suggestion_mv(
err.with_multipart_suggestion(
"you might have meant to write a const trait impl",
vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
Applicability::MaybeIncorrect,
@ -1457,7 +1457,7 @@ impl<'a> Parser<'a> {
if this.token == token::Not {
if let Err(err) = this.unexpected::<()>() {
err.note_mv(fluent::parse_macro_expands_to_enum_variant).emit();
err.with_note(fluent::parse_macro_expands_to_enum_variant).emit();
}
this.bump();
@ -1855,7 +1855,7 @@ impl<'a> Parser<'a> {
if eq_typo || semi_typo {
self.bump();
// Gracefully handle small typos.
err.span_suggestion_short_mv(
err.with_span_suggestion_short(
self.prev_token.span,
"field names and their types are separated with `:`",
":",
@ -1935,10 +1935,10 @@ impl<'a> Parser<'a> {
lo.to(self.prev_token.span),
format!("functions are not allowed in {adt_ty} definitions"),
)
.help_mv(
.with_help(
"unlike in C++, Java, and C#, functions are declared in `impl` blocks",
)
.help_mv("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
.with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
}
Err(err) => {
err.cancel();
@ -1954,7 +1954,9 @@ impl<'a> Parser<'a> {
lo.with_hi(ident.span.hi()),
format!("structs are not allowed in {adt_ty} definitions"),
)
.help_mv("consider creating a new `struct` definition instead of nesting"),
.with_help(
"consider creating a new `struct` definition instead of nesting",
),
Err(err) => {
err.cancel();
self.restore_snapshot(snapshot);

View file

@ -847,7 +847,7 @@ impl<'a> Parser<'a> {
pprust::token_to_string(&self.prev_token)
);
expect_err
.span_suggestion_verbose_mv(
.with_span_suggestion_verbose(
self.prev_token.span.shrink_to_hi().until(self.token.span),
msg,
" @ ",
@ -863,7 +863,7 @@ impl<'a> Parser<'a> {
// Parsed successfully, therefore most probably the code only
// misses a separator.
expect_err
.span_suggestion_short_mv(
.with_span_suggestion_short(
sp,
format!("missing `{token_str}`"),
token_str,

View file

@ -464,7 +464,7 @@ impl<'a> Parser<'a> {
self_
.dcx()
.struct_span_err(self_.token.span, msg)
.span_label_mv(self_.token.span, format!("expected {expected}"))
.with_span_label(self_.token.span, format!("expected {expected}"))
});
PatKind::Lit(self.mk_expr(lo, ExprKind::Lit(lit)))
} else {

View file

@ -128,7 +128,7 @@ impl<'a> Parser<'a> {
self.prev_token.span,
"found single colon before projection in qualified path",
)
.span_suggestion_mv(
.with_span_suggestion(
self.prev_token.span,
"use double colon",
"::",

View file

@ -1116,7 +1116,7 @@ impl<'a> Parser<'a> {
let before_fn_path = fn_path.span.shrink_to_lo();
self.dcx()
.struct_span_err(generic_args_span, "`Fn` traits cannot take lifetime parameters")
.multipart_suggestion_mv(
.with_multipart_suggestion(
"consider using a higher-ranked trait bound instead",
vec![(generic_args_span, "".to_owned()), (before_fn_path, snippet)],
Applicability::MaybeIncorrect,

View file

@ -208,7 +208,7 @@ fn emit_malformed_attribute(
} else {
sess.dcx
.struct_span_err(span, error_msg)
.span_suggestions_mv(
.with_span_suggestions(
span,
if suggestions.len() == 1 {
"must be of the form"