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

Remove StructuredDiag #127357

Merged
merged 3 commits into from
Jul 9, 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
4 changes: 0 additions & 4 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,6 @@ hir_analysis_paren_sugar_attribute = the `#[rustc_paren_sugar]` attribute is a t
hir_analysis_parenthesized_fn_trait_expansion =
parenthesized trait syntax expands to `{$expanded_type}`

hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
.suggestion = cast the value to `{$cast_ty}`
.help = cast the value to `{$cast_ty}`

hir_analysis_pattern_type_non_const_range = range patterns must have constant range start and end
hir_analysis_pattern_type_wild_pat = wildcard patterns are not permitted for pattern types
.label = this type is the same as the inner type without a pattern
Expand Down
15 changes: 1 addition & 14 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_middle::ty::Ty;
use rustc_span::{symbol::Ident, Span, Symbol};
mod pattern_types;
pub use pattern_types::*;
pub mod wrong_number_of_generic_args;

mod precise_captures;
pub(crate) use precise_captures::*;
Expand Down Expand Up @@ -692,20 +693,6 @@ pub(crate) struct TypeOf<'tcx> {
pub ty: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_pass_to_variadic_function, code = E0617)]
pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub cast_ty: &'a str,
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
pub sugg_span: Option<Span>,
pub replace: String,
#[help]
pub help: Option<()>,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_invalid_union_field, code = E0740)]
pub(crate) struct InvalidUnionField {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::structured_errors::StructuredDiag;
use rustc_errors::{codes::*, pluralize, Applicability, Diag, MultiSpan};
use rustc_errors::{
codes::*, pluralize, Applicability, Diag, Diagnostic, EmissionGuarantee, MultiSpan,
};
use rustc_hir as hir;
use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt};
use rustc_session::Session;
use rustc_span::def_id::DefId;
use std::iter;

Expand Down Expand Up @@ -541,14 +541,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

fn start_diagnostics(&self) -> Diag<'tcx> {
let span = self.path_segment.ident.span;
let msg = self.create_error_message();
self.tcx.dcx().struct_span_err(span, msg).with_code(self.code())
}

/// Builds the `expected 1 type argument / supplied 2 type arguments` message.
fn notify(&self, err: &mut Diag<'_>) {
fn notify(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
let (quantifier, bound) = self.get_quantifier_and_bound();
let provided_args = self.num_provided_args();

Expand Down Expand Up @@ -600,7 +594,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

fn suggest(&self, err: &mut Diag<'_>) {
fn suggest(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
debug!(
"suggest(self.provided {:?}, self.gen_args.span(): {:?})",
self.num_provided_args(),
Expand Down Expand Up @@ -628,7 +622,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
/// ```text
/// type Map = HashMap<String>;
/// ```
fn suggest_adding_args(&self, err: &mut Diag<'_>) {
fn suggest_adding_args(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
if self.gen_args.parenthesized != hir::GenericArgsParentheses::No {
return;
}
Expand All @@ -647,7 +641,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

fn suggest_adding_lifetime_args(&self, err: &mut Diag<'_>) {
fn suggest_adding_lifetime_args(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
debug!("suggest_adding_lifetime_args(path_segment: {:?})", self.path_segment);
let num_missing_args = self.num_missing_lifetime_args();
let num_params_to_take = num_missing_args;
Expand Down Expand Up @@ -701,7 +695,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

fn suggest_adding_type_and_const_args(&self, err: &mut Diag<'_>) {
fn suggest_adding_type_and_const_args(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
let num_missing_args = self.num_missing_type_or_const_args();
let msg = format!("add missing {} argument{}", self.kind(), pluralize!(num_missing_args));

Expand Down Expand Up @@ -761,7 +755,10 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
/// ```compile_fail
/// Into::into::<Option<_>>(42) // suggests considering `Into::<Option<_>>::into(42)`
/// ```
fn suggest_moving_args_from_assoc_fn_to_trait(&self, err: &mut Diag<'_>) {
fn suggest_moving_args_from_assoc_fn_to_trait(
&self,
err: &mut Diag<'_, impl EmissionGuarantee>,
) {
let trait_ = match self.tcx.trait_of_item(self.def_id) {
Some(def_id) => def_id,
None => return,
Expand Down Expand Up @@ -817,7 +814,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {

fn suggest_moving_args_from_assoc_fn_to_trait_for_qualified_path(
&self,
err: &mut Diag<'_>,
err: &mut Diag<'_, impl EmissionGuarantee>,
qpath: &'tcx hir::QPath<'tcx>,
msg: String,
num_assoc_fn_excess_args: usize,
Expand Down Expand Up @@ -850,7 +847,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {

fn suggest_moving_args_from_assoc_fn_to_trait_for_method_call(
&self,
err: &mut Diag<'_>,
err: &mut Diag<'_, impl EmissionGuarantee>,
trait_def_id: DefId,
expr: &'tcx hir::Expr<'tcx>,
msg: String,
Expand Down Expand Up @@ -904,7 +901,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
/// ```text
/// type Map = HashMap<String, String, String, String>;
/// ```
fn suggest_removing_args_or_generics(&self, err: &mut Diag<'_>) {
fn suggest_removing_args_or_generics(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
let num_provided_lt_args = self.num_provided_lifetime_args();
let num_provided_type_const_args = self.num_provided_type_or_const_args();
let unbound_types = self.get_unbound_associated_types();
Expand All @@ -922,7 +919,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let provided_args_matches_unbound_traits =
unbound_types.len() == num_redundant_type_or_const_args;

let remove_lifetime_args = |err: &mut Diag<'_>| {
let remove_lifetime_args = |err: &mut Diag<'_, _>| {
let mut lt_arg_spans = Vec::new();
let mut found_redundant = false;
for arg in self.gen_args.args {
Expand Down Expand Up @@ -963,7 +960,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
);
};

let remove_type_or_const_args = |err: &mut Diag<'_>| {
let remove_type_or_const_args = |err: &mut Diag<'_, _>| {
let mut gen_arg_spans = Vec::new();
let mut found_redundant = false;
for arg in self.gen_args.args {
Expand Down Expand Up @@ -1060,7 +1057,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}

/// Builds the `type defined here` message.
fn show_definition(&self, err: &mut Diag<'_>) {
fn show_definition(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
let mut spans: MultiSpan = if let Some(def_span) = self.tcx.def_ident_span(self.def_id) {
if self.tcx.sess.source_map().is_span_accessible(def_span) {
def_span.into()
Expand Down Expand Up @@ -1111,7 +1108,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}

/// Add note if `impl Trait` is explicitly specified.
fn note_synth_provided(&self, err: &mut Diag<'_>) {
fn note_synth_provided(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
if !self.is_synth_provided() {
return;
}
Expand All @@ -1120,17 +1117,16 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
}
}

impl<'tcx> StructuredDiag<'tcx> for WrongNumberOfGenericArgs<'_, 'tcx> {
fn session(&self) -> &Session {
self.tcx.sess
}

fn code(&self) -> ErrCode {
E0107
}

fn diagnostic_common(&self) -> Diag<'tcx> {
let mut err = self.start_diagnostics();
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for WrongNumberOfGenericArgs<'_, '_> {
fn into_diag(
self,
dcx: rustc_errors::DiagCtxtHandle<'a>,
level: rustc_errors::Level,
) -> Diag<'a, G> {
let msg = self.create_error_message();
let mut err = Diag::new(dcx, level, msg);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use translatable diagnostics, but that's something to do in a separate PR

err.code(E0107);
err.span(self.path_segment.ident.span);

self.notify(&mut err);
self.suggest(&mut err);
Expand Down
30 changes: 14 additions & 16 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::IsMethodCall;
use crate::errors::wrong_number_of_generic_args::{GenericArgsInfo, WrongNumberOfGenericArgs};
use crate::hir_ty_lowering::{
errors::prohibit_assoc_item_constraint, ExplicitLateBound, GenericArgCountMismatch,
GenericArgCountResult, GenericArgPosition, GenericArgsLowerer,
};
use crate::structured_errors::{GenericArgsInfo, StructuredDiag, WrongNumberOfGenericArgs};
use rustc_ast::ast::ParamKindOrd;
use rustc_errors::{
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
Expand Down Expand Up @@ -486,17 +486,15 @@ pub(crate) fn check_generic_arg_count(
GenericArgsInfo::MissingLifetimes { num_missing_args }
};

let reported = WrongNumberOfGenericArgs::new(
let reported = tcx.dcx().emit_err(WrongNumberOfGenericArgs::new(
tcx,
gen_args_info,
seg,
gen_params,
has_self as usize,
gen_args,
def_id,
)
.diagnostic()
.emit();
));

Err(reported)
};
Expand Down Expand Up @@ -573,17 +571,17 @@ pub(crate) fn check_generic_arg_count(
debug!(?gen_args_info);

let reported = gen_args.has_err().unwrap_or_else(|| {
WrongNumberOfGenericArgs::new(
tcx,
gen_args_info,
seg,
gen_params,
params_offset,
gen_args,
def_id,
)
.diagnostic()
.emit_unless(all_params_are_binded)
tcx.dcx()
.create_err(WrongNumberOfGenericArgs::new(
tcx,
gen_args_info,
seg,
gen_params,
params_offset,
gen_args,
def_id,
))
.emit_unless(all_params_are_binded)
});

Err(reported)
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ mod errors;
pub mod hir_wf_check;
mod impl_wf_check;
mod outlives;
pub mod structured_errors;
mod variance;

use rustc_hir as hir;
Expand Down
33 changes: 0 additions & 33 deletions compiler/rustc_hir_analysis/src/structured_errors.rs

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ hir_typeck_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_t

For more information about casts, take a look at The Book:
https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",

hir_typeck_cast_unknown_pointer = cannot cast {$to ->
[true] to
*[false] from
Expand Down Expand Up @@ -138,6 +139,11 @@ hir_typeck_option_result_asref = use `{$def_path}::as_ref` to convert `{$expecte
hir_typeck_option_result_cloned = use `{$def_path}::cloned` to clone the value inside the `{$def_path}`
hir_typeck_option_result_copied = use `{$def_path}::copied` to copy the value inside the `{$def_path}`

hir_typeck_pass_to_variadic_function = can't pass `{$ty}` to variadic function
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
.suggestion = cast the value to `{$cast_ty}`
.help = cast the value to `{$cast_ty}`
.teach_help = certain types, like `{$ty}`, must be casted before passing them to a variadic function, because of arcane ABI rules dictated by the C standard
oli-obk marked this conversation as resolved.
Show resolved Hide resolved

hir_typeck_ptr_cast_add_auto_to_object = adding {$traits_len ->
[1] an auto trait {$traits}
*[other] auto traits {$traits}
Expand Down
Loading
Loading