Skip to content

Commit ac1c6c5

Browse files
Use identifiers in diagnostics more often
1 parent f85c6de commit ac1c6c5

File tree

21 files changed

+83
-72
lines changed

21 files changed

+83
-72
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ fn try_report_async_mismatch<'tcx>(
23622362
// the right span is a bit difficult.
23632363
return Err(tcx.sess.dcx().emit_err(MethodShouldReturnFuture {
23642364
span: tcx.def_span(impl_m.def_id),
2365-
method_name: trait_m.name,
2365+
method_name: tcx.item_ident(impl_m.def_id),
23662366
trait_item_span: tcx.hir().span_if_local(trait_m.def_id),
23672367
}));
23682368
}

Diff for: compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
197197

198198
fn report_forbidden_specialization(tcx: TyCtxt<'_>, impl_item: DefId, parent_impl: DefId) {
199199
let span = tcx.def_span(impl_item);
200-
let ident = tcx.item_name(impl_item);
200+
let ident = tcx.item_ident(impl_item);
201201

202202
let err = match tcx.span_of_impl(parent_impl) {
203203
Ok(sp) => errors::ImplNotMarkedDefault::Ok { span, ident, ok_label: sp },
@@ -297,7 +297,7 @@ fn default_body_is_unstable(
297297
reason: Option<Symbol>,
298298
issue: Option<NonZero<u32>>,
299299
) {
300-
let missing_item_name = tcx.associated_item(item_did).name;
300+
let missing_item_name = tcx.item_ident(item_did);
301301
let (mut some_note, mut none_note, mut reason_str) = (false, false, String::new());
302302
match reason {
303303
Some(r) => {

Diff for: compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
292292

293293
res = Err(tcx.dcx().emit_err(errors::DispatchFromDynZST {
294294
span,
295-
name: field.name,
295+
name: field.ident(tcx),
296296
ty: ty_a,
297297
}));
298298

Diff for: compiler/rustc_hir_analysis/src/coherence/orphan.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ fn emit_orphan_check_error<'tcx>(
465465
traits::OrphanCheckErr::UncoveredTyParams(UncoveredTyParams { uncovered, local_ty }) => {
466466
let mut reported = None;
467467
for param_def_id in uncovered {
468-
let span = tcx.def_ident_span(param_def_id).unwrap();
469-
let name = tcx.item_name(param_def_id);
468+
let name = tcx.item_ident(param_def_id);
469+
let span = name.span;
470470

471471
reported.get_or_insert(match local_ty {
472472
Some(local_type) => tcx.dcx().emit_err(errors::TyParamFirstLocal {
@@ -492,7 +492,7 @@ fn lint_uncovered_ty_params<'tcx>(
492492

493493
for param_def_id in uncovered {
494494
let span = tcx.def_ident_span(param_def_id).unwrap();
495-
let name = tcx.item_name(param_def_id);
495+
let name = tcx.item_ident(param_def_id);
496496

497497
match local_ty {
498498
Some(local_type) => tcx.emit_node_span_lint(

Diff for: compiler/rustc_hir_analysis/src/collect.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ fn lower_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
928928
tcx.dcx().emit_err(errors::EnumDiscriminantOverflowed {
929929
span,
930930
discr: prev_discr.unwrap().to_string(),
931-
item_name: tcx.item_name(variant.def_id),
931+
item_name: tcx.item_ident(variant.def_id),
932932
wrapped_discr: wrapped_discr.to_string(),
933933
});
934934
None
@@ -990,11 +990,10 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> {
990990
}
991991

992992
/// Check if a given field `ident` declared at `field_decl` has been declared elsewhere before.
993-
fn check_field_decl(&mut self, ident: Ident, field_decl: FieldDeclSpan) {
993+
fn check_field_decl(&mut self, field_name: Ident, field_decl: FieldDeclSpan) {
994994
use FieldDeclSpan::*;
995-
let field_name = ident.name;
996-
let ident = ident.normalize_to_macros_2_0();
997-
match (field_decl, self.seen_fields.get(&ident).copied()) {
995+
let field_name = field_name.normalize_to_macros_2_0();
996+
match (field_decl, self.seen_fields.get(&field_name).copied()) {
998997
(NotNested(span), Some(NotNested(prev_span))) => {
999998
self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::NotNested {
1000999
field_name,
@@ -1035,7 +1034,7 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> {
10351034
});
10361035
}
10371036
(field_decl, None) => {
1038-
self.seen_fields.insert(ident, field_decl);
1037+
self.seen_fields.insert(field_name, field_decl);
10391038
}
10401039
}
10411040
}

Diff for: compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
5555
} else {
5656
let reported = tcx.dcx().emit_err(UnconstrainedOpaqueType {
5757
span: tcx.def_span(def_id),
58-
name: tcx.item_name(parent_def_id.to_def_id()),
58+
name: tcx.item_ident(parent_def_id.to_def_id()),
5959
what: "impl",
6060
});
6161
Ty::new_error(tcx, reported)
@@ -136,7 +136,7 @@ pub(super) fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: Local
136136
}
137137
let reported = tcx.dcx().emit_err(UnconstrainedOpaqueType {
138138
span: tcx.def_span(def_id),
139-
name: tcx.item_name(parent_def_id.to_def_id()),
139+
name: tcx.item_ident(parent_def_id.to_def_id()),
140140
what: match tcx.hir_node(scope) {
141141
_ if scope == hir::CRATE_HIR_ID => "module",
142142
Node::Item(hir::Item { kind: hir::ItemKind::Mod(_), .. }) => "module",

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub(crate) struct DropImplOnWrongItem {
217217
pub(crate) enum FieldAlreadyDeclared {
218218
#[diag(hir_analysis_field_already_declared, code = E0124)]
219219
NotNested {
220-
field_name: Symbol,
220+
field_name: Ident,
221221
#[primary_span]
222222
#[label]
223223
span: Span,
@@ -226,7 +226,7 @@ pub(crate) enum FieldAlreadyDeclared {
226226
},
227227
#[diag(hir_analysis_field_already_declared_current_nested)]
228228
CurrentNested {
229-
field_name: Symbol,
229+
field_name: Ident,
230230
#[primary_span]
231231
#[label]
232232
span: Span,
@@ -239,7 +239,7 @@ pub(crate) enum FieldAlreadyDeclared {
239239
},
240240
#[diag(hir_analysis_field_already_declared_previous_nested)]
241241
PreviousNested {
242-
field_name: Symbol,
242+
field_name: Ident,
243243
#[primary_span]
244244
#[label]
245245
span: Span,
@@ -252,7 +252,7 @@ pub(crate) enum FieldAlreadyDeclared {
252252
},
253253
#[diag(hir_analysis_field_already_declared_both_nested)]
254254
BothNested {
255-
field_name: Symbol,
255+
field_name: Ident,
256256
#[primary_span]
257257
#[label]
258258
span: Span,
@@ -418,7 +418,7 @@ pub(crate) struct ValueOfAssociatedStructAlreadySpecified {
418418
pub(crate) struct UnconstrainedOpaqueType {
419419
#[primary_span]
420420
pub span: Span,
421-
pub name: Symbol,
421+
pub name: Ident,
422422
pub what: &'static str,
423423
}
424424

@@ -802,7 +802,7 @@ pub(crate) struct EnumDiscriminantOverflowed {
802802
#[label]
803803
pub span: Span,
804804
pub discr: String,
805-
pub item_name: Symbol,
805+
pub item_name: Ident,
806806
pub wrapped_discr: String,
807807
}
808808

@@ -893,15 +893,15 @@ pub(crate) enum ImplNotMarkedDefault {
893893
span: Span,
894894
#[label(hir_analysis_ok_label)]
895895
ok_label: Span,
896-
ident: Symbol,
896+
ident: Ident,
897897
},
898898
#[diag(hir_analysis_impl_not_marked_default_err, code = E0520)]
899899
#[note]
900900
Err {
901901
#[primary_span]
902902
span: Span,
903903
cname: Symbol,
904-
ident: Symbol,
904+
ident: Ident,
905905
},
906906
}
907907

@@ -977,7 +977,7 @@ pub(crate) struct MissingTraitItemUnstable {
977977
pub some_note: bool,
978978
#[note(hir_analysis_none_note)]
979979
pub none_note: bool,
980-
pub missing_item_name: Symbol,
980+
pub missing_item_name: Ident,
981981
pub feature: Symbol,
982982
pub reason: String,
983983
}
@@ -1249,7 +1249,7 @@ pub(crate) struct InherentNominal {
12491249
pub(crate) struct DispatchFromDynZST<'a> {
12501250
#[primary_span]
12511251
pub span: Span,
1252-
pub name: Symbol,
1252+
pub name: Ident,
12531253
pub ty: Ty<'a>,
12541254
}
12551255

@@ -1389,7 +1389,7 @@ pub(crate) struct TyParamFirstLocal<'tcx> {
13891389
pub span: Span,
13901390
#[note(hir_analysis_case_note)]
13911391
pub note: (),
1392-
pub param: Symbol,
1392+
pub param: Ident,
13931393
pub local_type: Ty<'tcx>,
13941394
}
13951395

@@ -1401,7 +1401,7 @@ pub(crate) struct TyParamFirstLocalLint<'tcx> {
14011401
pub span: Span,
14021402
#[note(hir_analysis_case_note)]
14031403
pub note: (),
1404-
pub param: Symbol,
1404+
pub param: Ident,
14051405
pub local_type: Ty<'tcx>,
14061406
}
14071407

@@ -1414,7 +1414,7 @@ pub(crate) struct TyParamSome {
14141414
pub span: Span,
14151415
#[note(hir_analysis_only_note)]
14161416
pub note: (),
1417-
pub param: Symbol,
1417+
pub param: Ident,
14181418
}
14191419

14201420
#[derive(LintDiagnostic)]
@@ -1425,7 +1425,7 @@ pub(crate) struct TyParamSomeLint {
14251425
pub span: Span,
14261426
#[note(hir_analysis_only_note)]
14271427
pub note: (),
1428-
pub param: Symbol,
1428+
pub param: Ident,
14291429
}
14301430

14311431
#[derive(Diagnostic)]
@@ -1533,7 +1533,7 @@ pub(crate) struct UnsupportedDelegation<'a> {
15331533
pub(crate) struct MethodShouldReturnFuture {
15341534
#[primary_span]
15351535
pub span: Span,
1536-
pub method_name: Symbol,
1536+
pub method_name: Ident,
15371537
#[note]
15381538
pub trait_item_span: Option<Span>,
15391539
}
@@ -1585,7 +1585,7 @@ pub(crate) struct UnconstrainedGenericParameter {
15851585
#[primary_span]
15861586
#[label]
15871587
pub span: Span,
1588-
pub param_name: Symbol,
1588+
pub param_name: Ident,
15891589
pub param_def_kind: &'static str,
15901590
#[note(hir_analysis_const_param_note)]
15911591
pub const_param_note: bool,

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub(crate) fn enforce_impl_lifetime_params_are_constrained(
152152
{
153153
let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter {
154154
span: tcx.def_span(param.def_id),
155-
param_name: param.name,
155+
param_name: tcx.item_ident(param.def_id),
156156
param_def_kind: tcx.def_descr(param.def_id),
157157
const_param_note: false,
158158
const_param_note2: false,
@@ -223,7 +223,7 @@ pub(crate) fn enforce_impl_non_lifetime_params_are_constrained(
223223
let const_param_note = matches!(param.kind, ty::GenericParamDefKind::Const { .. });
224224
let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter {
225225
span: tcx.def_span(param.def_id),
226-
param_name: param.name,
226+
param_name: tcx.item_ident(param.def_id),
227227
param_def_kind: tcx.def_descr(param.def_id),
228228
const_param_note,
229229
const_param_note2: const_param_note,

Diff for: compiler/rustc_lint/src/dangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
141141
expr.hir_id,
142142
method.ident.span,
143143
DanglingPointersFromTemporaries {
144-
callee: method.ident.name,
144+
callee: method.ident,
145145
ty,
146146
ptr_span: method.ident.span,
147147
temporary_span: receiver.span,

Diff for: compiler/rustc_lint/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ pub(crate) struct IgnoredUnlessCrateSpecified<'a> {
11321132
#[help(lint_help_visit)]
11331133
// FIXME: put #[primary_span] on `ptr_span` once it does not cause conflicts
11341134
pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
1135-
pub callee: Symbol,
1135+
pub callee: Ident,
11361136
pub ty: Ty<'tcx>,
11371137
#[label(lint_label_ptr)]
11381138
pub ptr_span: Span,
@@ -1333,7 +1333,7 @@ pub(crate) enum NonUpperCaseGlobalSub {
13331333
#[diag(lint_noop_method_call)]
13341334
#[note]
13351335
pub(crate) struct NoopMethodCallDiag<'a> {
1336-
pub method: Symbol,
1336+
pub method: Ident,
13371337
pub orig_ty: Ty<'a>,
13381338
pub trait_: Symbol,
13391339
#[suggestion(code = "", applicability = "machine-applicable")]

Diff for: compiler/rustc_lint/src/noop_method_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
129129
_ => None,
130130
};
131131
cx.emit_span_lint(NOOP_METHOD_CALL, span, NoopMethodCallDiag {
132-
method: call.ident.name,
132+
method: call.ident,
133133
orig_ty,
134134
trait_,
135135
label: span,

Diff for: compiler/rustc_middle/src/ty/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,15 @@ impl<'tcx> TyCtxt<'tcx> {
15961596
Some(Ident::new(def, span))
15971597
}
15981598

1599+
/// Look up the name and span of a definition.
1600+
///
1601+
/// See [`item_name`][Self::item_name] for more information.
1602+
pub fn item_ident(self, def_id: DefId) -> Ident {
1603+
self.opt_item_ident(def_id).unwrap_or_else(|| {
1604+
bug!("item_ident: no name for {:?}", self.def_path(def_id));
1605+
})
1606+
}
1607+
15991608
pub fn opt_associated_item(self, def_id: DefId) -> Option<AssocItem> {
16001609
if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) {
16011610
Some(self.associated_item(def_id))

Diff for: compiler/rustc_mir_build/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
77
use rustc_middle::ty::{self, Ty};
88
use rustc_pattern_analysis::errors::Uncovered;
99
use rustc_pattern_analysis::rustc::RustcPatCtxt;
10-
use rustc_span::{Span, Symbol};
10+
use rustc_span::{Ident, Span, Symbol};
1111

1212
use crate::fluent_generated as fluent;
1313

@@ -753,7 +753,7 @@ pub(crate) struct BindingsWithVariantName {
753753
#[suggestion(code = "{ty_path}::{name}", applicability = "machine-applicable")]
754754
pub(crate) suggestion: Option<Span>,
755755
pub(crate) ty_path: String,
756-
pub(crate) name: Symbol,
756+
pub(crate) name: Ident,
757757
}
758758

759759
#[derive(LintDiagnostic)]
@@ -797,7 +797,7 @@ pub(crate) struct BorrowOfMovedValue {
797797
pub(crate) binding_span: Span,
798798
#[label(mir_build_value_borrowed_label)]
799799
pub(crate) conflicts_ref: Vec<Span>,
800-
pub(crate) name: Symbol,
800+
pub(crate) name: Ident,
801801
pub(crate) ty: String,
802802
#[suggestion(code = "ref ", applicability = "machine-applicable")]
803803
pub(crate) suggest_borrowing: Option<Span>,

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_session::lint::builtin::{
2525
};
2626
use rustc_span::edit_distance::find_best_match_for_name;
2727
use rustc_span::hygiene::DesugaringKind;
28-
use rustc_span::{Span, sym};
28+
use rustc_span::{Ident, Span, sym};
2929
use rustc_trait_selection::infer::InferCtxtExt;
3030
use tracing::instrument;
3131

@@ -800,7 +800,7 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, 'tcx>, pat:
800800
sess.dcx().emit_err(BorrowOfMovedValue {
801801
binding_span: pat.span,
802802
conflicts_ref,
803-
name,
803+
name: Ident::new(name, pat.span),
804804
ty,
805805
suggest_borrowing: Some(pat.span.shrink_to_lo()),
806806
has_path: path.is_some(),
@@ -908,7 +908,7 @@ fn check_for_bindings_named_same_as_variants(
908908
None
909909
},
910910
ty_path,
911-
name,
911+
name: Ident::new(name, pat.span),
912912
},
913913
)
914914
}

Diff for: compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3233,7 +3233,7 @@ pub(crate) struct MalformedCfgAttr {
32333233
pub(crate) struct UnknownBuiltinConstruct {
32343234
#[primary_span]
32353235
pub span: Span,
3236-
pub name: Symbol,
3236+
pub name: Ident,
32373237
}
32383238

32393239
#[derive(Diagnostic)]

Diff for: compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ impl<'a> Parser<'a> {
19581958
} else {
19591959
let err = self.dcx().create_err(errors::UnknownBuiltinConstruct {
19601960
span: lo.to(ident.span),
1961-
name: ident.name,
1961+
name: ident,
19621962
});
19631963
return Err(err);
19641964
};

0 commit comments

Comments
 (0)