Skip to content

Commit 3396d1d

Browse files
committed
Don't look at static items' HIR for wfcheck
1 parent 2ba45d4 commit 3396d1d

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,6 +4862,10 @@ impl<'hir> Node<'hir> {
48624862
ImplItemKind::Type(ty) => Some(ty),
48634863
_ => None,
48644864
},
4865+
Node::ForeignItem(it) => match it.kind {
4866+
ForeignItemKind::Static(ty, ..) => Some(ty),
4867+
_ => None,
4868+
},
48654869
_ => None,
48664870
}
48674871
}

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
729729
}
730730
}
731731

732-
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
732+
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
733733
let generics = tcx.generics_of(def_id);
734734

735735
for param in &generics.own_params {
@@ -757,6 +757,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
757757
DefKind::Static { .. } => {
758758
check_static_inhabited(tcx, def_id);
759759
check_static_linkage(tcx, def_id);
760+
wfcheck::check_static_item(tcx, def_id)?;
760761
}
761762
DefKind::Const => {}
762763
DefKind::Enum => {
@@ -774,13 +775,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
774775
}
775776
DefKind::Impl { of_trait } => {
776777
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
777-
if tcx
778-
.ensure_ok()
779-
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)
780-
.is_ok()
781-
{
782-
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
783-
}
778+
tcx.ensure_ok()
779+
.coherent_trait(impl_trait_header.trait_ref.instantiate_identity().def_id)?;
780+
781+
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
784782
}
785783
}
786784
DefKind::Trait => {
@@ -838,7 +836,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
838836
DefKind::ForeignMod => {
839837
let it = tcx.hir_expect_item(def_id);
840838
let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
841-
return;
839+
return Ok(());
842840
};
843841

844842
check_abi(tcx, it.hir_id(), it.span, abi);
@@ -896,6 +894,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
896894
}
897895
_ => {}
898896
}
897+
Ok(())
899898
}
900899

901900
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, def_id: LocalDefId) {

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,17 @@ where
185185
}
186186

187187
fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
188-
crate::check::check::check_item_type(tcx, def_id);
188+
let mut res = crate::check::check::check_item_type(tcx, def_id);
189189
let node = tcx.hir_node_by_def_id(def_id);
190-
let mut res = match node {
190+
res = res.and(match node {
191191
hir::Node::Crate(_) => bug!("check_well_formed cannot be applied to the crate root"),
192192
hir::Node::Item(item) => check_item(tcx, item),
193193
hir::Node::TraitItem(item) => check_trait_item(tcx, item),
194194
hir::Node::ImplItem(item) => check_impl_item(tcx, item),
195195
hir::Node::ForeignItem(item) => check_foreign_item(tcx, item),
196196
hir::Node::ConstBlock(_) | hir::Node::Expr(_) | hir::Node::OpaqueTy(_) => Ok(()),
197197
_ => unreachable!("{node:?}"),
198-
};
198+
});
199199

200200
for param in &tcx.generics_of(def_id).own_params {
201201
res = res.and(check_param_wf(tcx, param));
@@ -291,9 +291,6 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
291291
res
292292
}
293293
hir::ItemKind::Fn { ident, sig, .. } => check_item_fn(tcx, def_id, ident, sig.decl),
294-
hir::ItemKind::Static(_, _, ty, _) => {
295-
check_static_item(tcx, def_id, ty.span, UnsizedHandling::Forbid)
296-
}
297294
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span, item.span),
298295
hir::ItemKind::Struct(_, generics, _) => {
299296
let res = check_type_defn(tcx, item, false);
@@ -347,10 +344,7 @@ fn check_foreign_item<'tcx>(
347344

348345
match item.kind {
349346
hir::ForeignItemKind::Fn(sig, ..) => check_item_fn(tcx, def_id, item.ident, sig.decl),
350-
hir::ForeignItemKind::Static(ty, ..) => {
351-
check_static_item(tcx, def_id, ty.span, UnsizedHandling::AllowIfForeignTail)
352-
}
353-
hir::ForeignItemKind::Type => Ok(()),
347+
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => Ok(()),
354348
}
355349
}
356350

@@ -1247,61 +1241,52 @@ fn check_item_fn(
12471241
})
12481242
}
12491243

1250-
enum UnsizedHandling {
1251-
Forbid,
1252-
AllowIfForeignTail,
1253-
}
1254-
1255-
#[instrument(level = "debug", skip(tcx, ty_span, unsized_handling))]
1256-
fn check_static_item(
1244+
#[instrument(level = "debug", skip(tcx))]
1245+
pub(super) fn check_static_item(
12571246
tcx: TyCtxt<'_>,
12581247
item_id: LocalDefId,
1259-
ty_span: Span,
1260-
unsized_handling: UnsizedHandling,
12611248
) -> Result<(), ErrorGuaranteed> {
12621249
enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
12631250
let ty = tcx.type_of(item_id).instantiate_identity();
1264-
let item_ty = wfcx.deeply_normalize(ty_span, Some(WellFormedLoc::Ty(item_id)), ty);
1265-
1266-
let forbid_unsized = match unsized_handling {
1267-
UnsizedHandling::Forbid => true,
1268-
UnsizedHandling::AllowIfForeignTail => {
1269-
let tail =
1270-
tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
1271-
!matches!(tail.kind(), ty::Foreign(_))
1272-
}
1251+
let item_ty = wfcx.deeply_normalize(DUMMY_SP, Some(WellFormedLoc::Ty(item_id)), ty);
1252+
1253+
let is_foreign_item = tcx.is_foreign_item(item_id);
1254+
1255+
let forbid_unsized = !is_foreign_item || {
1256+
let tail = tcx.struct_tail_for_codegen(item_ty, wfcx.infcx.typing_env(wfcx.param_env));
1257+
!matches!(tail.kind(), ty::Foreign(_))
12731258
};
12741259

1275-
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
1260+
wfcx.register_wf_obligation(DUMMY_SP, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
12761261
if forbid_unsized {
12771262
wfcx.register_bound(
12781263
traits::ObligationCause::new(
1279-
ty_span,
1264+
DUMMY_SP,
12801265
wfcx.body_def_id,
12811266
ObligationCauseCode::SizedConstOrStatic,
12821267
),
12831268
wfcx.param_env,
12841269
item_ty,
1285-
tcx.require_lang_item(LangItem::Sized, ty_span),
1270+
tcx.require_lang_item(LangItem::Sized, tcx.def_span(item_id)),
12861271
);
12871272
}
12881273

12891274
// Ensure that the end result is `Sync` in a non-thread local `static`.
12901275
let should_check_for_sync = tcx.static_mutability(item_id.to_def_id())
12911276
== Some(hir::Mutability::Not)
1292-
&& !tcx.is_foreign_item(item_id.to_def_id())
1277+
&& !is_foreign_item
12931278
&& !tcx.is_thread_local_static(item_id.to_def_id());
12941279

12951280
if should_check_for_sync {
12961281
wfcx.register_bound(
12971282
traits::ObligationCause::new(
1298-
ty_span,
1283+
DUMMY_SP,
12991284
wfcx.body_def_id,
13001285
ObligationCauseCode::SharedStatic,
13011286
),
13021287
wfcx.param_env,
13031288
item_ty,
1304-
tcx.require_lang_item(LangItem::Sync, ty_span),
1289+
tcx.require_lang_item(LangItem::Sync, DUMMY_SP),
13051290
);
13061291
}
13071292
Ok(())

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::ty::{
3131
};
3232
use rustc_middle::{bug, span_bug};
3333
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
34-
use tracing::{debug, instrument};
34+
use tracing::{debug, instrument, trace};
3535

3636
use super::on_unimplemented::{AppendConstMessage, OnUnimplementedNote};
3737
use super::suggestions::get_explanation_based_on_obligation;
@@ -67,6 +67,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
6767

6868
let mut err = match *error {
6969
SelectionError::Unimplemented => {
70+
trace!(?root_obligation.cause);
7071
// If this obligation was generated as a result of well-formedness checking, see if we
7172
// can get a better error message by performing HIR-based well-formedness checking.
7273
if let ObligationCauseCode::WellFormed(Some(wf_loc)) =
@@ -81,6 +82,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
8182
span = obligation.cause.span;
8283
}
8384
}
85+
if let ObligationCauseCode::SizedConstOrStatic | ObligationCauseCode::SharedStatic = root_obligation.cause.code() {
86+
let node = tcx.hir_node_by_def_id(root_obligation.cause.body_id);
87+
span = node.ty().unwrap_or_else(|| panic!("{node:?} had no type")).span;
88+
obligation.cause.span = span;
89+
trace!("patched up span")
90+
}
8491

8592
if let ObligationCauseCode::CompareImplItem {
8693
impl_item_def_id,

0 commit comments

Comments
 (0)