Skip to content

Commit 57edf88

Browse files
committed
Replace some trivial struct_span_err!s in typeck.
1 parent 93eaf15 commit 57edf88

File tree

10 files changed

+254
-194
lines changed

10 files changed

+254
-194
lines changed

compiler/rustc_typeck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ doctest = false
1111
[dependencies]
1212
rustc_arena = { path = "../rustc_arena" }
1313
tracing = "0.1"
14+
rustc_macros = { path = "../rustc_macros" }
1415
rustc_middle = { path = "../rustc_middle" }
1516
rustc_attr = { path = "../rustc_attr" }
1617
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_typeck/src/astconv/generics.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::astconv::{
22
AstConv, ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgPosition,
33
};
4+
use crate::errors::AssocTypeBindingNotAllowed;
45
use rustc_ast::ast::ParamKindOrd;
56
use rustc_errors::{pluralize, struct_span_err, DiagnosticId, ErrorReported};
67
use rustc_hir as hir;
@@ -544,13 +545,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
544545

545546
/// Emits an error regarding forbidden type binding associations
546547
pub fn prohibit_assoc_ty_binding(tcx: TyCtxt<'_>, span: Span) {
547-
let mut err = struct_span_err!(
548-
tcx.sess,
549-
span,
550-
E0229,
551-
"associated type bindings are not allowed here"
552-
);
553-
err.span_label(span, "associated type not allowed here").emit();
548+
tcx.sess.emit_err(AssocTypeBindingNotAllowed { span });
554549
}
555550

556551
/// Prohibits explicit lifetime arguments if late-bound lifetime parameters

compiler/rustc_typeck/src/astconv/mod.rs

+14-43
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ mod generics;
77

88
use crate::bounds::Bounds;
99
use crate::collect::PlaceholderHirTyCollector;
10+
use crate::errors::{
11+
AmbiguousLifetimeBound, MultipleRelaxedDefaultBounds, TraitObjectDeclaredWithNoTraits,
12+
TypeofReservedKeywordUsed, ValueOfAssociatedStructAlreadySpecified,
13+
};
1014
use crate::middle::resolve_lifetime as rl;
1115
use crate::require_c_abi_if_c_variadic;
1216
use rustc_ast::util::lev_distance::find_best_match_for_name;
@@ -684,14 +688,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
684688
if unbound.is_none() {
685689
unbound = Some(&ptr.trait_ref);
686690
} else {
687-
struct_span_err!(
688-
tcx.sess,
689-
span,
690-
E0203,
691-
"type parameter has more than one relaxed default \
692-
bound, only one is supported"
693-
)
694-
.emit();
691+
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
695692
}
696693
}
697694
}
@@ -927,18 +924,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
927924
dup_bindings
928925
.entry(assoc_ty.def_id)
929926
.and_modify(|prev_span| {
930-
struct_span_err!(
931-
self.tcx().sess,
932-
binding.span,
933-
E0719,
934-
"the value of the associated type `{}` (from trait `{}`) \
935-
is already specified",
936-
binding.item_name,
937-
tcx.def_path_str(assoc_ty.container.id())
938-
)
939-
.span_label(binding.span, "re-bound here")
940-
.span_label(*prev_span, format!("`{}` bound here first", binding.item_name))
941-
.emit();
927+
self.tcx().sess.emit_err(ValueOfAssociatedStructAlreadySpecified {
928+
span: binding.span,
929+
prev_span: *prev_span,
930+
item_name: binding.item_name,
931+
def_path: tcx.def_path_str(assoc_ty.container.id()),
932+
});
942933
})
943934
.or_insert(binding.span);
944935
}
@@ -1051,13 +1042,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10511042
}
10521043

10531044
if regular_traits.is_empty() && auto_traits.is_empty() {
1054-
struct_span_err!(
1055-
tcx.sess,
1056-
span,
1057-
E0224,
1058-
"at least one trait is required for an object type"
1059-
)
1060-
.emit();
1045+
tcx.sess.emit_err(TraitObjectDeclaredWithNoTraits { span });
10611046
return tcx.ty_error();
10621047
}
10631048

@@ -2059,15 +2044,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20592044
self.normalize_ty(ast_ty.span, array_ty)
20602045
}
20612046
hir::TyKind::Typeof(ref _e) => {
2062-
struct_span_err!(
2063-
tcx.sess,
2064-
ast_ty.span,
2065-
E0516,
2066-
"`typeof` is a reserved keyword but unimplemented"
2067-
)
2068-
.span_label(ast_ty.span, "reserved keyword")
2069-
.emit();
2070-
2047+
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
20712048
tcx.ty_error()
20722049
}
20732050
hir::TyKind::Infer => {
@@ -2283,13 +2260,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22832260
// error.
22842261
let r = derived_region_bounds[0];
22852262
if derived_region_bounds[1..].iter().any(|r1| r != *r1) {
2286-
struct_span_err!(
2287-
tcx.sess,
2288-
span,
2289-
E0227,
2290-
"ambiguous lifetime bound, explicit lifetime bound required"
2291-
)
2292-
.emit();
2263+
tcx.sess.emit_err(AmbiguousLifetimeBound { span });
22932264
}
22942265
Some(r)
22952266
}

compiler/rustc_typeck/src/check/compare_method.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
12
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
23
use rustc_hir as hir;
34
use rustc_hir::def::{DefKind, Res};
@@ -366,24 +367,18 @@ fn check_region_bounds_on_impl_item<'tcx>(
366367
let item_kind = assoc_item_kind_str(impl_m);
367368
let def_span = tcx.sess.source_map().guess_head_span(span);
368369
let span = tcx.hir().get_generics(impl_m.def_id).map(|g| g.span).unwrap_or(def_span);
369-
let mut err = struct_span_err!(
370-
tcx.sess,
370+
let generics_span = if let Some(sp) = tcx.hir().span_if_local(trait_m.def_id) {
371+
let def_sp = tcx.sess.source_map().guess_head_span(sp);
372+
Some(tcx.hir().get_generics(trait_m.def_id).map(|g| g.span).unwrap_or(def_sp))
373+
} else {
374+
None
375+
};
376+
tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait {
371377
span,
372-
E0195,
373-
"lifetime parameters or bounds on {} `{}` do not match the trait declaration",
374378
item_kind,
375-
impl_m.ident,
376-
);
377-
err.span_label(span, &format!("lifetimes do not match {} in trait", item_kind));
378-
if let Some(sp) = tcx.hir().span_if_local(trait_m.def_id) {
379-
let def_sp = tcx.sess.source_map().guess_head_span(sp);
380-
let sp = tcx.hir().get_generics(trait_m.def_id).map(|g| g.span).unwrap_or(def_sp);
381-
err.span_label(
382-
sp,
383-
&format!("lifetimes in impl do not match this {} in trait", item_kind),
384-
);
385-
}
386-
err.emit();
379+
ident: impl_m.ident,
380+
generics_span,
381+
});
387382
return Err(ErrorReported);
388383
}
389384

compiler/rustc_typeck/src/check/expr.rs

+20-49
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ use crate::check::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExp
1414
use crate::check::FnCtxt;
1515
use crate::check::Needs;
1616
use crate::check::TupleArgumentsFlag::DontTupleArguments;
17+
use crate::errors::{
18+
FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
19+
YieldExprOutsideOfGenerator,
20+
};
1721
use crate::type_error_struct;
1822

23+
use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
1924
use rustc_ast as ast;
2025
use rustc_ast::util::lev_distance::find_best_match_for_name;
2126
use rustc_data_structures::fx::FxHashMap;
@@ -439,14 +444,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439444
})
440445
});
441446
if !is_named {
442-
struct_span_err!(
443-
self.tcx.sess,
444-
oprnd.span,
445-
E0745,
446-
"cannot take address of a temporary"
447-
)
448-
.span_label(oprnd.span, "temporary value")
449-
.emit();
447+
self.tcx.sess.emit_err(AddressOfTemporaryTaken { span: oprnd.span })
450448
}
451449
}
452450

@@ -665,13 +663,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
665663
expr: &'tcx hir::Expr<'tcx>,
666664
) -> Ty<'tcx> {
667665
if self.ret_coercion.is_none() {
668-
struct_span_err!(
669-
self.tcx.sess,
670-
expr.span,
671-
E0572,
672-
"return statement outside of function body",
673-
)
674-
.emit();
666+
self.tcx.sess.emit_err(ReturnStmtOutsideOfFnBody { span: expr.span });
675667
} else if let Some(ref e) = expr_opt {
676668
if self.ret_coercion_span.borrow().is_none() {
677669
*self.ret_coercion_span.borrow_mut() = Some(e.span);
@@ -740,6 +732,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
740732
expr_span: &Span,
741733
) {
742734
if !lhs.is_syntactic_place_expr() {
735+
// FIXME: Make this use SessionDiagnostic once error codes can be dynamically set.
743736
let mut err = self.tcx.sess.struct_span_err_with_code(
744737
*expr_span,
745738
"invalid left-hand side of assignment",
@@ -1120,14 +1113,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11201113
// Prohibit struct expressions when non-exhaustive flag is set.
11211114
let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
11221115
if !adt.did.is_local() && variant.is_field_list_non_exhaustive() {
1123-
struct_span_err!(
1124-
self.tcx.sess,
1125-
expr.span,
1126-
E0639,
1127-
"cannot create non-exhaustive {} using struct expression",
1128-
adt.variant_descr()
1129-
)
1130-
.emit();
1116+
self.tcx
1117+
.sess
1118+
.emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
11311119
}
11321120

11331121
let error_happened = self.check_expr_struct_fields(
@@ -1165,13 +1153,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11651153
.insert(expr.hir_id, fru_field_types);
11661154
}
11671155
_ => {
1168-
struct_span_err!(
1169-
self.tcx.sess,
1170-
base_expr.span,
1171-
E0436,
1172-
"functional record update syntax requires a struct"
1173-
)
1174-
.emit();
1156+
self.tcx
1157+
.sess
1158+
.emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
11751159
}
11761160
}
11771161
}
@@ -1234,18 +1218,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12341218
} else {
12351219
error_happened = true;
12361220
if let Some(prev_span) = seen_fields.get(&ident) {
1237-
let mut err = struct_span_err!(
1238-
self.tcx.sess,
1239-
field.ident.span,
1240-
E0062,
1241-
"field `{}` specified more than once",
1242-
ident
1243-
);
1244-
1245-
err.span_label(field.ident.span, "used more than once");
1246-
err.span_label(*prev_span, format!("first use of `{}`", ident));
1247-
1248-
err.emit();
1221+
tcx.sess.emit_err(FieldMultiplySpecifiedInInitializer {
1222+
span: field.ident.span,
1223+
prev_span: *prev_span,
1224+
ident,
1225+
});
12491226
} else {
12501227
self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name, span);
12511228
}
@@ -1876,13 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18761853
self.tcx.mk_unit()
18771854
}
18781855
_ => {
1879-
struct_span_err!(
1880-
self.tcx.sess,
1881-
expr.span,
1882-
E0627,
1883-
"yield expression outside of generator literal"
1884-
)
1885-
.emit();
1856+
self.tcx.sess.emit_err(YieldExprOutsideOfGenerator { span: expr.span });
18861857
self.tcx.mk_unit()
18871858
}
18881859
}

compiler/rustc_typeck/src/check/intrinsic.rs

+11-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Type-checking for the rust-intrinsic and platform-intrinsic
22
//! intrinsics that the compiler exposes.
33
4+
use crate::errors::{
5+
SimdShuffleMissingLength, UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
6+
WrongNumberOfTypeArgumentsToInstrinsic,
7+
};
48
use crate::require_same_types;
59

610
use rustc_errors::struct_span_err;
@@ -41,17 +45,11 @@ fn equate_intrinsic_type<'tcx>(
4145
_ => bug!(),
4246
};
4347

44-
struct_span_err!(
45-
tcx.sess,
48+
tcx.sess.emit_err(WrongNumberOfTypeArgumentsToInstrinsic {
4649
span,
47-
E0094,
48-
"intrinsic has wrong number of type \
49-
parameters: found {}, expected {}",
50-
i_n_tps,
51-
n_tps
52-
)
53-
.span_label(span, format!("expected {} type parameter", n_tps))
54-
.emit();
50+
found: i_n_tps,
51+
expected: n_tps,
52+
});
5553
return;
5654
}
5755

@@ -146,15 +144,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
146144
| "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)),
147145
"fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()),
148146
op => {
149-
struct_span_err!(
150-
tcx.sess,
151-
it.span,
152-
E0092,
153-
"unrecognized atomic operation function: `{}`",
154-
op
155-
)
156-
.span_label(it.span, "unrecognized atomic operation")
157-
.emit();
147+
tcx.sess.emit_err(UnrecognizedAtomicOperation { span: it.span, op });
158148
return;
159149
}
160150
};
@@ -380,15 +370,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
380370
sym::nontemporal_store => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
381371

382372
other => {
383-
struct_span_err!(
384-
tcx.sess,
385-
it.span,
386-
E0093,
387-
"unrecognized intrinsic function: `{}`",
388-
other,
389-
)
390-
.span_label(it.span, "unrecognized intrinsic")
391-
.emit();
373+
tcx.sess.emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
392374
return;
393375
}
394376
};
@@ -468,14 +450,7 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>)
468450
(2, params, param(1))
469451
}
470452
Err(_) => {
471-
struct_span_err!(
472-
tcx.sess,
473-
it.span,
474-
E0439,
475-
"invalid `simd_shuffle`, needs length: `{}`",
476-
name
477-
)
478-
.emit();
453+
tcx.sess.emit_err(SimdShuffleMissingLength { span: it.span, name });
479454
return;
480455
}
481456
}

0 commit comments

Comments
 (0)