Skip to content
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

Diagnostic API fixes #119751

Merged
merged 7 commits into from
Jan 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! gate {
($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain)
.help_mv($help)
.with_help($help)
.emit();
}
}};
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_passes/src/show_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ struct ShowSpanVisitor<'a> {
impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
fn visit_expr(&mut self, e: &'a ast::Expr) {
if let Mode::Expression = self.mode {
self.dcx.emit_warning(errors::ShowSpan { span: e.span, msg: "expression" });
self.dcx.emit_warn(errors::ShowSpan { span: e.span, msg: "expression" });
}
visit::walk_expr(self, e);
}

fn visit_pat(&mut self, p: &'a ast::Pat) {
if let Mode::Pattern = self.mode {
self.dcx.emit_warning(errors::ShowSpan { span: p.span, msg: "pattern" });
self.dcx.emit_warn(errors::ShowSpan { span: p.span, msg: "pattern" });
}
visit::walk_pat(self, p);
}

fn visit_ty(&mut self, t: &'a ast::Ty) {
if let Mode::Type = self.mode {
self.dcx.emit_warning(errors::ShowSpan { span: t.span, msg: "type" });
self.dcx.emit_warn(errors::ShowSpan { span: t.span, msg: "type" });
}
visit::walk_ty(self, t);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ pub fn eval_condition(
}
};
let Some(min_version) = parse_version(*min_version) else {
dcx.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
dcx.emit_warn(session_diagnostics::UnknownVersionLiteral { span: *span });
return false;
};

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_attr/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item)
.span_mv(self.span)
.code_mv(error_code!(E0541))
.arg_mv("item", self.item)
.arg_mv("expected", expected.join(", "))
.span_label_mv(self.span, fluent::attr_label)
.with_span(self.span)
.with_code(error_code!(E0541))
.with_arg("item", self.item)
.with_arg("expected", expected.join(", "))
.with_span_label(self.span, fluent::attr_label)
}
}

Expand Down
85 changes: 49 additions & 36 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_errors::{struct_span_err, DiagCtxt, DiagnosticBuilder};
use rustc_errors::{struct_span_code_err, DiagCtxt, DiagnosticBuilder};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;

Expand Down Expand Up @@ -31,15 +31,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
borrow_span: Span,
borrow_desc: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
span,
E0503,
"cannot use {} because it was mutably borrowed",
desc,
)
.span_label_mv(borrow_span, format!("{borrow_desc} is borrowed here"))
.span_label_mv(span, format!("use of borrowed {borrow_desc}"))
.with_span_label(borrow_span, format!("{borrow_desc} is borrowed here"))
.with_span_label(span, format!("use of borrowed {borrow_desc}"))
}

pub(crate) fn cannot_mutably_borrow_multiply(
Expand All @@ -52,7 +52,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
old_load_end_span: Option<Span>,
) -> DiagnosticBuilder<'tcx> {
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
E0499,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
old_loan_span: Span,
old_load_end_span: Option<Span>,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
E0524,
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
old_opt_via: &str,
previous_end_span: Option<Span>,
) -> DiagnosticBuilder<'cx> {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
E0500,
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
previous_end_span: Option<Span>,
second_borrow_desc: &str,
) -> DiagnosticBuilder<'cx> {
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
E0501,
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
old_load_end_span: Option<Span>,
) -> DiagnosticBuilder<'cx> {
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
let mut err = struct_span_err!(
let mut err = struct_span_code_err!(
self.dcx(),
span,
E0502,
Expand Down Expand Up @@ -236,15 +236,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
borrow_span: Span,
desc: &str,
) -> DiagnosticBuilder<'cx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
span,
E0506,
"cannot assign to {} because it is borrowed",
desc,
)
.span_label_mv(borrow_span, format!("{desc} is borrowed here"))
.span_label_mv(span, format!("{desc} is assigned to here but it was already borrowed"))
.with_span_label(borrow_span, format!("{desc} is borrowed here"))
.with_span_label(span, format!("{desc} is assigned to here but it was already borrowed"))
}

pub(crate) fn cannot_reassign_immutable(
Expand All @@ -254,19 +254,25 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
is_arg: bool,
) -> DiagnosticBuilder<'cx> {
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
struct_span_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
struct_span_code_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
}

pub(crate) fn cannot_assign(&self, span: Span, desc: &str) -> DiagnosticBuilder<'tcx> {
struct_span_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
struct_span_code_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
}

pub(crate) fn cannot_move_out_of(
&self,
move_from_span: Span,
move_from_desc: &str,
) -> DiagnosticBuilder<'cx> {
struct_span_err!(self.dcx(), move_from_span, E0507, "cannot move out of {}", move_from_desc)
struct_span_code_err!(
self.dcx(),
move_from_span,
E0507,
"cannot move out of {}",
move_from_desc
)
}

/// Signal an error due to an attempt to move out of the interior
Expand All @@ -283,30 +289,30 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
(&ty::Slice(_), _) => "slice",
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
};
struct_span_err!(
struct_span_code_err!(
self.dcx(),
move_from_span,
E0508,
"cannot move out of type `{}`, a non-copy {}",
ty,
type_name,
)
.span_label_mv(move_from_span, "cannot move out of here")
.with_span_label(move_from_span, "cannot move out of here")
}

pub(crate) fn cannot_move_out_of_interior_of_drop(
&self,
move_from_span: Span,
container_ty: Ty<'_>,
) -> DiagnosticBuilder<'cx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
move_from_span,
E0509,
"cannot move out of type `{}`, which implements the `Drop` trait",
container_ty,
)
.span_label_mv(move_from_span, "cannot move out of here")
.with_span_label(move_from_span, "cannot move out of here")
}

pub(crate) fn cannot_act_on_moved_value(
Expand All @@ -318,7 +324,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
) -> DiagnosticBuilder<'tcx> {
let moved_path = moved_path.map(|mp| format!(": `{mp}`")).unwrap_or_default();

struct_span_err!(
struct_span_code_err!(
self.dcx(),
use_span,
E0382,
Expand All @@ -335,7 +341,14 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
path: &str,
reason: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(self.dcx(), span, E0596, "cannot borrow {} as mutable{}", path, reason)
struct_span_code_err!(
self.dcx(),
span,
E0596,
"cannot borrow {} as mutable{}",
path,
reason
)
}

pub(crate) fn cannot_mutate_in_immutable_section(
Expand All @@ -346,7 +359,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
immutable_section: &str,
action: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
mutate_span,
E0510,
Expand All @@ -355,8 +368,8 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
immutable_place,
immutable_section,
)
.span_label_mv(mutate_span, format!("cannot {action}"))
.span_label_mv(immutable_span, format!("value is immutable in {immutable_section}"))
.with_span_label(mutate_span, format!("cannot {action}"))
.with_span_label(immutable_span, format!("value is immutable in {immutable_section}"))
}

pub(crate) fn cannot_borrow_across_coroutine_yield(
Expand All @@ -365,20 +378,20 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
yield_span: Span,
) -> DiagnosticBuilder<'tcx> {
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
struct_span_err!(
struct_span_code_err!(
self.dcx(),
span,
E0626,
"borrow may still be in use when {coroutine_kind:#} yields",
)
.span_label_mv(yield_span, "possible yield occurs here")
.with_span_label(yield_span, "possible yield occurs here")
}

pub(crate) fn cannot_borrow_across_destructor(
&self,
borrow_span: Span,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
borrow_span,
E0713,
Expand All @@ -391,7 +404,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
span: Span,
path: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
}

pub(crate) fn cannot_return_reference_to_local(
Expand All @@ -401,7 +414,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
reference_desc: &str,
path_desc: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
span,
E0515,
Expand All @@ -410,7 +423,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
REFERENCE = reference_desc,
LOCAL = path_desc,
)
.span_label_mv(
.with_span_label(
span,
format!("{return_kind}s a {reference_desc} data owned by the current function"),
)
Expand All @@ -424,22 +437,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
capture_span: Span,
scope: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
closure_span,
E0373,
"{closure_kind} may outlive the current {scope}, but it borrows {borrowed_path}, \
which is owned by the current {scope}",
)
.span_label_mv(capture_span, format!("{borrowed_path} is borrowed here"))
.span_label_mv(closure_span, format!("may outlive borrowed value {borrowed_path}"))
.with_span_label(capture_span, format!("{borrowed_path} is borrowed here"))
.with_span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"))
}

pub(crate) fn thread_local_value_does_not_live_long_enough(
&self,
span: Span,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
self.dcx(),
span,
E0712,
Expand All @@ -451,7 +464,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
&self,
span: Span,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
}
}

Expand All @@ -460,7 +473,7 @@ pub(crate) fn borrowed_data_escapes_closure<'tcx>(
escape_span: Span,
escapes_from: &str,
) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
struct_span_code_err!(
tcx.dcx(),
escape_span,
E0521,
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// ignore-tidy-filelength

use either::Either;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
use rustc_errors::{struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
Expand Down Expand Up @@ -550,8 +552,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
};

let used = desired_action.as_general_verb_in_past_tense();
let mut err =
struct_span_err!(self.dcx(), span, E0381, "{used} binding {desc}{isnt_initialized}");
let mut err = struct_span_code_err!(
self.dcx(),
span,
E0381,
"{used} binding {desc}{isnt_initialized}"
);
use_spans.var_path_only_subdiag(&mut err, desired_action);

if let InitializationRequiringAction::PartialAssignment
Expand Down Expand Up @@ -2219,11 +2225,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
);

self.thread_local_value_does_not_live_long_enough(borrow_span)
.span_label_mv(
.with_span_label(
borrow_span,
"thread-local variables cannot be borrowed beyond the end of the function",
)
.span_label_mv(drop_span, "end of enclosing function is here")
.with_span_label(drop_span, "end of enclosing function is here")
}

#[instrument(level = "debug", skip(self))]
Expand Down
Loading
Loading