Skip to content

Commit cd3d98b

Browse files
committed
Auto merge of rust-lang#127357 - oli-obk:structureddiag, r=fmease
Remove `StructuredDiag` follow-up to rust-lang#127319 This trait was an experiment that didn't pan out.
2 parents 99b7134 + af9ab1b commit cd3d98b

File tree

10 files changed

+81
-161
lines changed

10 files changed

+81
-161
lines changed

compiler/rustc_hir_analysis/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,6 @@ hir_analysis_paren_sugar_attribute = the `#[rustc_paren_sugar]` attribute is a t
373373
hir_analysis_parenthesized_fn_trait_expansion =
374374
parenthesized trait syntax expands to `{$expanded_type}`
375375
376-
hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
377-
.suggestion = cast the value to `{$cast_ty}`
378-
.help = cast the value to `{$cast_ty}`
379-
380376
hir_analysis_pattern_type_non_const_range = range patterns must have constant range start and end
381377
hir_analysis_pattern_type_wild_pat = wildcard patterns are not permitted for pattern types
382378
.label = this type is the same as the inner type without a pattern

compiler/rustc_hir_analysis/src/errors.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::ty::Ty;
99
use rustc_span::{symbol::Ident, Span, Symbol};
1010
mod pattern_types;
1111
pub use pattern_types::*;
12+
pub mod wrong_number_of_generic_args;
1213

1314
mod precise_captures;
1415
pub(crate) use precise_captures::*;
@@ -692,20 +693,6 @@ pub(crate) struct TypeOf<'tcx> {
692693
pub ty: Ty<'tcx>,
693694
}
694695

695-
#[derive(Diagnostic)]
696-
#[diag(hir_analysis_pass_to_variadic_function, code = E0617)]
697-
pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
698-
#[primary_span]
699-
pub span: Span,
700-
pub ty: Ty<'tcx>,
701-
pub cast_ty: &'a str,
702-
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
703-
pub sugg_span: Option<Span>,
704-
pub replace: String,
705-
#[help]
706-
pub help: Option<()>,
707-
}
708-
709696
#[derive(Diagnostic)]
710697
#[diag(hir_analysis_invalid_union_field, code = E0740)]
711698
pub(crate) struct InvalidUnionField {

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs renamed to compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::structured_errors::StructuredDiag;
2-
use rustc_errors::{codes::*, pluralize, Applicability, Diag, MultiSpan};
1+
use rustc_errors::{
2+
codes::*, pluralize, Applicability, Diag, Diagnostic, EmissionGuarantee, MultiSpan,
3+
};
34
use rustc_hir as hir;
45
use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt};
5-
use rustc_session::Session;
66
use rustc_span::def_id::DefId;
77
use std::iter;
88

@@ -541,14 +541,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
541541
}
542542
}
543543

544-
fn start_diagnostics(&self) -> Diag<'tcx> {
545-
let span = self.path_segment.ident.span;
546-
let msg = self.create_error_message();
547-
self.tcx.dcx().struct_span_err(span, msg).with_code(self.code())
548-
}
549-
550544
/// Builds the `expected 1 type argument / supplied 2 type arguments` message.
551-
fn notify(&self, err: &mut Diag<'_>) {
545+
fn notify(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
552546
let (quantifier, bound) = self.get_quantifier_and_bound();
553547
let provided_args = self.num_provided_args();
554548

@@ -600,7 +594,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
600594
}
601595
}
602596

603-
fn suggest(&self, err: &mut Diag<'_>) {
597+
fn suggest(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
604598
debug!(
605599
"suggest(self.provided {:?}, self.gen_args.span(): {:?})",
606600
self.num_provided_args(),
@@ -628,7 +622,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
628622
/// ```text
629623
/// type Map = HashMap<String>;
630624
/// ```
631-
fn suggest_adding_args(&self, err: &mut Diag<'_>) {
625+
fn suggest_adding_args(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
632626
if self.gen_args.parenthesized != hir::GenericArgsParentheses::No {
633627
return;
634628
}
@@ -647,7 +641,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
647641
}
648642
}
649643

650-
fn suggest_adding_lifetime_args(&self, err: &mut Diag<'_>) {
644+
fn suggest_adding_lifetime_args(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
651645
debug!("suggest_adding_lifetime_args(path_segment: {:?})", self.path_segment);
652646
let num_missing_args = self.num_missing_lifetime_args();
653647
let num_params_to_take = num_missing_args;
@@ -701,7 +695,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
701695
}
702696
}
703697

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

@@ -761,7 +755,10 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
761755
/// ```compile_fail
762756
/// Into::into::<Option<_>>(42) // suggests considering `Into::<Option<_>>::into(42)`
763757
/// ```
764-
fn suggest_moving_args_from_assoc_fn_to_trait(&self, err: &mut Diag<'_>) {
758+
fn suggest_moving_args_from_assoc_fn_to_trait(
759+
&self,
760+
err: &mut Diag<'_, impl EmissionGuarantee>,
761+
) {
765762
let trait_ = match self.tcx.trait_of_item(self.def_id) {
766763
Some(def_id) => def_id,
767764
None => return,
@@ -817,7 +814,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
817814

818815
fn suggest_moving_args_from_assoc_fn_to_trait_for_qualified_path(
819816
&self,
820-
err: &mut Diag<'_>,
817+
err: &mut Diag<'_, impl EmissionGuarantee>,
821818
qpath: &'tcx hir::QPath<'tcx>,
822819
msg: String,
823820
num_assoc_fn_excess_args: usize,
@@ -850,7 +847,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
850847

851848
fn suggest_moving_args_from_assoc_fn_to_trait_for_method_call(
852849
&self,
853-
err: &mut Diag<'_>,
850+
err: &mut Diag<'_, impl EmissionGuarantee>,
854851
trait_def_id: DefId,
855852
expr: &'tcx hir::Expr<'tcx>,
856853
msg: String,
@@ -904,7 +901,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
904901
/// ```text
905902
/// type Map = HashMap<String, String, String, String>;
906903
/// ```
907-
fn suggest_removing_args_or_generics(&self, err: &mut Diag<'_>) {
904+
fn suggest_removing_args_or_generics(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
908905
let num_provided_lt_args = self.num_provided_lifetime_args();
909906
let num_provided_type_const_args = self.num_provided_type_or_const_args();
910907
let unbound_types = self.get_unbound_associated_types();
@@ -922,7 +919,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
922919
let provided_args_matches_unbound_traits =
923920
unbound_types.len() == num_redundant_type_or_const_args;
924921

925-
let remove_lifetime_args = |err: &mut Diag<'_>| {
922+
let remove_lifetime_args = |err: &mut Diag<'_, _>| {
926923
let mut lt_arg_spans = Vec::new();
927924
let mut found_redundant = false;
928925
for arg in self.gen_args.args {
@@ -963,7 +960,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
963960
);
964961
};
965962

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

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

11131110
/// Add note if `impl Trait` is explicitly specified.
1114-
fn note_synth_provided(&self, err: &mut Diag<'_>) {
1111+
fn note_synth_provided(&self, err: &mut Diag<'_, impl EmissionGuarantee>) {
11151112
if !self.is_synth_provided() {
11161113
return;
11171114
}
@@ -1120,17 +1117,16 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
11201117
}
11211118
}
11221119

1123-
impl<'tcx> StructuredDiag<'tcx> for WrongNumberOfGenericArgs<'_, 'tcx> {
1124-
fn session(&self) -> &Session {
1125-
self.tcx.sess
1126-
}
1127-
1128-
fn code(&self) -> ErrCode {
1129-
E0107
1130-
}
1131-
1132-
fn diagnostic_common(&self) -> Diag<'tcx> {
1133-
let mut err = self.start_diagnostics();
1120+
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for WrongNumberOfGenericArgs<'_, '_> {
1121+
fn into_diag(
1122+
self,
1123+
dcx: rustc_errors::DiagCtxtHandle<'a>,
1124+
level: rustc_errors::Level,
1125+
) -> Diag<'a, G> {
1126+
let msg = self.create_error_message();
1127+
let mut err = Diag::new(dcx, level, msg);
1128+
err.code(E0107);
1129+
err.span(self.path_segment.ident.span);
11341130

11351131
self.notify(&mut err);
11361132
self.suggest(&mut err);

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::IsMethodCall;
2+
use crate::errors::wrong_number_of_generic_args::{GenericArgsInfo, WrongNumberOfGenericArgs};
23
use crate::hir_ty_lowering::{
34
errors::prohibit_assoc_item_constraint, ExplicitLateBound, GenericArgCountMismatch,
45
GenericArgCountResult, GenericArgPosition, GenericArgsLowerer,
56
};
6-
use crate::structured_errors::{GenericArgsInfo, StructuredDiag, WrongNumberOfGenericArgs};
77
use rustc_ast::ast::ParamKindOrd;
88
use rustc_errors::{
99
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
@@ -486,17 +486,15 @@ pub(crate) fn check_generic_arg_count(
486486
GenericArgsInfo::MissingLifetimes { num_missing_args }
487487
};
488488

489-
let reported = WrongNumberOfGenericArgs::new(
489+
let reported = tcx.dcx().emit_err(WrongNumberOfGenericArgs::new(
490490
tcx,
491491
gen_args_info,
492492
seg,
493493
gen_params,
494494
has_self as usize,
495495
gen_args,
496496
def_id,
497-
)
498-
.diagnostic()
499-
.emit();
497+
));
500498

501499
Err(reported)
502500
};
@@ -573,17 +571,17 @@ pub(crate) fn check_generic_arg_count(
573571
debug!(?gen_args_info);
574572

575573
let reported = gen_args.has_err().unwrap_or_else(|| {
576-
WrongNumberOfGenericArgs::new(
577-
tcx,
578-
gen_args_info,
579-
seg,
580-
gen_params,
581-
params_offset,
582-
gen_args,
583-
def_id,
584-
)
585-
.diagnostic()
586-
.emit_unless(all_params_are_binded)
574+
tcx.dcx()
575+
.create_err(WrongNumberOfGenericArgs::new(
576+
tcx,
577+
gen_args_info,
578+
seg,
579+
gen_params,
580+
params_offset,
581+
gen_args,
582+
def_id,
583+
))
584+
.emit_unless(all_params_are_binded)
587585
});
588586

589587
Err(reported)

compiler/rustc_hir_analysis/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ mod errors;
9292
pub mod hir_wf_check;
9393
mod impl_wf_check;
9494
mod outlives;
95-
pub mod structured_errors;
9695
mod variance;
9796

9897
use rustc_hir as hir;

compiler/rustc_hir_analysis/src/structured_errors.rs

-33
This file was deleted.

compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs

-57
This file was deleted.

compiler/rustc_hir_typeck/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ hir_typeck_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_t
3838
3939
For more information about casts, take a look at The Book:
4040
https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions",
41+
4142
hir_typeck_cast_unknown_pointer = cannot cast {$to ->
4243
[true] to
4344
*[false] from
@@ -138,6 +139,11 @@ hir_typeck_option_result_asref = use `{$def_path}::as_ref` to convert `{$expecte
138139
hir_typeck_option_result_cloned = use `{$def_path}::cloned` to clone the value inside the `{$def_path}`
139140
hir_typeck_option_result_copied = use `{$def_path}::copied` to copy the value inside the `{$def_path}`
140141
142+
hir_typeck_pass_to_variadic_function = can't pass `{$ty}` to variadic function
143+
.suggestion = cast the value to `{$cast_ty}`
144+
.help = cast the value to `{$cast_ty}`
145+
.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
146+
141147
hir_typeck_ptr_cast_add_auto_to_object = adding {$traits_len ->
142148
[1] an auto trait {$traits}
143149
*[other] auto traits {$traits}

0 commit comments

Comments
 (0)