Skip to content

Commit 03fdcff

Browse files
Rollup merge of rust-lang#136114 - compiler-errors:more-idents, r=jieyouxu
Use identifiers more in diagnostics code This should make the diagnostics code slightly more correct when rendering idents in mixed crate edition situations. Kinda a no-op, but a cleanup regardless. r? oli-obk or reassign
2 parents f4a5cbd + c08624d commit 03fdcff

File tree

31 files changed

+97
-90
lines changed

31 files changed

+97
-90
lines changed

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
157157
{
158158
cx.dcx().emit_err(RequiresMaybeSized {
159159
span: pointee_ty_ident.span,
160-
name: pointee_ty_ident.name.to_ident_string(),
160+
name: pointee_ty_ident,
161161
});
162162
return;
163163
}
@@ -471,5 +471,5 @@ struct TooManyPointees {
471471
struct RequiresMaybeSized {
472472
#[primary_span]
473473
span: Span,
474-
name: String,
474+
name: Ident,
475475
}

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
}

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) => {

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

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(

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
}

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",

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,

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
495495
.iter()
496496
.any(|constraint| constraint.ident.name == item.name)
497497
})
498-
.map(|item| item.name.to_ident_string())
498+
.map(|item| self.tcx.item_ident(item.def_id).to_string())
499499
.collect()
500500
} else {
501501
Vec::default()

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,

compiler/rustc_hir_typeck/src/expr.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -3337,10 +3337,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33373337
})
33383338
.map(|mut field_path| {
33393339
field_path.pop();
3340-
field_path
3341-
.iter()
3342-
.map(|id| format!("{}.", id.name.to_ident_string()))
3343-
.collect::<String>()
3340+
field_path.iter().map(|id| format!("{}.", id)).collect::<String>()
33443341
})
33453342
.collect::<Vec<_>>();
33463343
candidate_fields.sort();

compiler/rustc_hir_typeck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ fn report_unexpected_variant_res(
453453
);
454454
let fields = fields
455455
.iter()
456-
.map(|field| format!("{}: _", field.name.to_ident_string()))
456+
.map(|field| format!("{}: _", field.ident(tcx)))
457457
.collect::<Vec<_>>()
458458
.join(", ");
459459
let sugg = format!(" {{ {} }}", fields);

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27142714
.map(|field_path| {
27152715
field_path
27162716
.iter()
2717-
.map(|id| id.name.to_ident_string())
2717+
.map(|id| id.to_string())
27182718
.collect::<Vec<String>>()
27192719
.join(".")
27202720
})

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,

compiler/rustc_lint/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ pub(crate) struct IgnoredUnlessCrateSpecified<'a> {
11501150
#[help(lint_help_visit)]
11511151
// FIXME: put #[primary_span] on `ptr_span` once it does not cause conflicts
11521152
pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
1153-
pub callee: Symbol,
1153+
pub callee: Ident,
11541154
pub ty: Ty<'tcx>,
11551155
#[label(lint_label_ptr)]
11561156
pub ptr_span: Span,
@@ -1351,7 +1351,7 @@ pub(crate) enum NonUpperCaseGlobalSub {
13511351
#[diag(lint_noop_method_call)]
13521352
#[note]
13531353
pub(crate) struct NoopMethodCallDiag<'a> {
1354-
pub method: Symbol,
1354+
pub method: Ident,
13551355
pub orig_ty: Ty<'a>,
13561356
pub trait_: Symbol,
13571357
#[suggestion(code = "", applicability = "machine-applicable")]

compiler/rustc_lint/src/non_local_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,5 @@ fn path_span_without_args(path: &Path<'_>) -> Span {
343343

344344
/// Return a "error message-able" ident for the last segment of the `Path`
345345
fn path_name_to_string(path: &Path<'_>) -> String {
346-
path.segments.last().unwrap().ident.name.to_ident_string()
346+
path.segments.last().unwrap().ident.to_string()
347347
}

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,

compiler/rustc_lint/src/pass_by_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
4545
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
4646
match path.res {
4747
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
48-
let name = cx.tcx.item_name(def_id).to_ident_string();
48+
let name = cx.tcx.item_ident(def_id);
4949
let path_segment = path.segments.last().unwrap();
5050
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
5151
}

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))

0 commit comments

Comments
 (0)