Skip to content

Commit c5a0939

Browse files
authored
Rollup merge of rust-lang#120619 - compiler-errors:param, r=lcnr
Assert that params with the same *index* have the same *name* Found this bug when trying to build libcore with the new solver, since it will canonicalize two params with the same index into *different* placeholders if those params differ by name.
2 parents 83bc10b + 1abd0da commit c5a0939

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{codes::*, struct_span_code_err, DiagnosticMessage};
1212
use rustc_hir as hir;
1313
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
15-
use rustc_span::symbol::{kw, sym, Symbol};
15+
use rustc_span::symbol::{kw, sym};
1616
use rustc_target::spec::abi::Abi;
1717

1818
fn equate_intrinsic_type<'tcx>(
@@ -132,7 +132,17 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: DefId) -> hir
132132
/// Remember to add all intrinsics here, in `compiler/rustc_codegen_llvm/src/intrinsic.rs`,
133133
/// and in `library/core/src/intrinsics.rs`.
134134
pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
135-
let param = |n| Ty::new_param(tcx, n, Symbol::intern(&format!("P{n}")));
135+
let generics = tcx.generics_of(it.owner_id);
136+
let param = |n| {
137+
if let Some(&ty::GenericParamDef {
138+
name, kind: ty::GenericParamDefKind::Type { .. }, ..
139+
}) = generics.opt_param_at(n as usize, tcx)
140+
{
141+
Ty::new_param(tcx, n, name)
142+
} else {
143+
Ty::new_error_with_message(tcx, tcx.def_span(it.owner_id), "expected param")
144+
}
145+
};
136146
let intrinsic_id = it.owner_id.to_def_id();
137147
let intrinsic_name = tcx.item_name(intrinsic_id);
138148
let name_str = intrinsic_name.as_str();
@@ -475,9 +485,16 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
475485

476486
/// Type-check `extern "platform-intrinsic" { ... }` functions.
477487
pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
488+
let generics = tcx.generics_of(it.owner_id);
478489
let param = |n| {
479-
let name = Symbol::intern(&format!("P{n}"));
480-
Ty::new_param(tcx, n, name)
490+
if let Some(&ty::GenericParamDef {
491+
name, kind: ty::GenericParamDefKind::Type { .. }, ..
492+
}) = generics.opt_param_at(n as usize, tcx)
493+
{
494+
Ty::new_param(tcx, n, name)
495+
} else {
496+
Ty::new_error_with_message(tcx, tcx.def_span(it.owner_id), "expected param")
497+
}
481498
};
482499

483500
let name = it.ident.name;

compiler/rustc_middle/src/ty/relate.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,10 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>(
435435
Ok(a)
436436
}
437437

438-
(ty::Param(a_p), ty::Param(b_p)) if a_p.index == b_p.index => Ok(a),
438+
(ty::Param(a_p), ty::Param(b_p)) if a_p.index == b_p.index => {
439+
debug_assert_eq!(a_p.name, b_p.name, "param types with same index differ in name");
440+
Ok(a)
441+
}
439442

440443
(ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a),
441444

@@ -586,7 +589,10 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>(
586589
(ty::ConstKind::Error(_), _) => return Ok(a),
587590
(_, ty::ConstKind::Error(_)) => return Ok(b),
588591

589-
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) => a_p.index == b_p.index,
592+
(ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index == b_p.index => {
593+
debug_assert_eq!(a_p.name, b_p.name, "param types with same index differ in name");
594+
true
595+
}
590596
(ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
591597
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => a_val == b_val,
592598

0 commit comments

Comments
 (0)