Skip to content

Commit f2f6bec

Browse files
committed
Auto merge of #143433 - oli-obk:ty_span_qry, r=<try>
Add `ty_span` query r? `@compiler-errors` fixes diagnostic regressions from #142030 Also uses the new query in `check_const_item`
2 parents 556d20a + ccbaf66 commit f2f6bec

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,15 +768,14 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
768768
check_static_inhabited(tcx, def_id);
769769
check_static_linkage(tcx, def_id);
770770
res = res.and(wfcheck::check_static_item(tcx, def_id));
771-
772-
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
773-
// checks. Returning early here does not miss any checks and
774-
// avoids this query from having a direct dependency edge on the HIR
775-
return res;
776771
}
777-
DefKind::Const => {}
772+
DefKind::Const => res = res.and(wfcheck::check_const_item(tcx, def_id)),
778773
_ => unreachable!(),
779774
}
775+
// Only `Node::Item` and `Node::ForeignItem` still have HIR based
776+
// checks. Returning early here does not miss any checks and
777+
// avoids this query from having a direct dependency edge on the HIR
778+
return res;
780779
}
781780
DefKind::Enum => {
782781
tcx.ensure_ok().generics_of(def_id);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ pub(super) fn check_item<'tcx>(
290290
res
291291
}
292292
hir::ItemKind::Fn { sig, .. } => check_item_fn(tcx, def_id, sig.decl),
293-
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span),
294293
hir::ItemKind::Struct(..) => check_type_defn(tcx, item, false),
295294
hir::ItemKind::Union(..) => check_type_defn(tcx, item, true),
296295
hir::ItemKind::Enum(..) => check_type_defn(tcx, item, true),
@@ -1185,7 +1184,8 @@ pub(super) fn check_static_item(
11851184
) -> Result<(), ErrorGuaranteed> {
11861185
enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
11871186
let ty = tcx.type_of(item_id).instantiate_identity();
1188-
let item_ty = wfcx.deeply_normalize(DUMMY_SP, Some(WellFormedLoc::Ty(item_id)), ty);
1187+
let span = tcx.ty_span(item_id);
1188+
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
11891189

11901190
let is_foreign_item = tcx.is_foreign_item(item_id);
11911191

@@ -1194,7 +1194,7 @@ pub(super) fn check_static_item(
11941194
!matches!(tail.kind(), ty::Foreign(_))
11951195
};
11961196

1197-
wfcx.register_wf_obligation(DUMMY_SP, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
1197+
wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
11981198
if forbid_unsized {
11991199
let span = tcx.def_span(item_id);
12001200
wfcx.register_bound(
@@ -1216,7 +1216,6 @@ pub(super) fn check_static_item(
12161216
&& !tcx.is_thread_local_static(item_id.to_def_id());
12171217

12181218
if should_check_for_sync {
1219-
let span = tcx.def_span(item_id);
12201219
wfcx.register_bound(
12211220
traits::ObligationCause::new(
12221221
span,
@@ -1232,13 +1231,10 @@ pub(super) fn check_static_item(
12321231
})
12331232
}
12341233

1235-
fn check_const_item(
1236-
tcx: TyCtxt<'_>,
1237-
def_id: LocalDefId,
1238-
ty_span: Span,
1239-
) -> Result<(), ErrorGuaranteed> {
1234+
pub(crate) fn check_const_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
12401235
enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
12411236
let ty = tcx.type_of(def_id).instantiate_identity();
1237+
let ty_span = tcx.ty_span(def_id);
12421238
let ty = wfcx.deeply_normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty);
12431239

12441240
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into());
@@ -1505,7 +1501,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
15051501
let cause = traits::ObligationCause::new(
15061502
sp,
15071503
wfcx.body_def_id,
1508-
ObligationCauseCode::WhereClause(def_id.to_def_id(), DUMMY_SP),
1504+
ObligationCauseCode::WhereClause(def_id.to_def_id(), sp),
15091505
);
15101506
Obligation::new(tcx, cause, wfcx.param_env, pred)
15111507
});

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,16 @@ pub fn provide(providers: &mut Providers) {
239239
let hir_id = tcx.local_def_id_to_hir_id(def_id);
240240
tcx.hir_opt_ident_span(hir_id)
241241
};
242+
providers.ty_span = |tcx, def_id| {
243+
let node = tcx.hir_node_by_def_id(def_id);
244+
match node.ty() {
245+
Some(ty) => ty.span,
246+
None => bug!("{def_id:?} doesn't have a type: {node:#?}"),
247+
}
248+
};
242249
providers.fn_arg_idents = |tcx, def_id| {
243-
if let Some(body_id) = tcx.hir_node_by_def_id(def_id).body_id() {
250+
let node = tcx.hir_node_by_def_id(def_id);
251+
if let Some(body_id) = node.body_id() {
244252
tcx.arena.alloc_from_iter(tcx.hir_body_param_idents(body_id))
245253
} else if let Node::TraitItem(&TraitItem {
246254
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
@@ -249,7 +257,7 @@ pub fn provide(providers: &mut Providers) {
249257
| Node::ForeignItem(&ForeignItem {
250258
kind: ForeignItemKind::Fn(_, idents, _),
251259
..
252-
}) = tcx.hir_node(tcx.local_def_id_to_hir_id(def_id))
260+
}) = node
253261
{
254262
idents
255263
} else {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,13 @@ rustc_queries! {
14521452
feedable
14531453
}
14541454

1455+
/// Gets the span for the type of the definition.
1456+
/// Panics if it is not a definition that has a single type.
1457+
query ty_span(def_id: LocalDefId) -> Span {
1458+
desc { |tcx| "looking up span for `{}`'s type", tcx.def_path_str(def_id) }
1459+
cache_on_disk_if { true }
1460+
}
1461+
14551462
query lookup_stability(def_id: DefId) -> Option<attr::Stability> {
14561463
desc { |tcx| "looking up stability of `{}`", tcx.def_path_str(def_id) }
14571464
cache_on_disk_if { def_id.is_local() }

tests/ui/issues/issue-7364.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `RefCell<isize>` cannot be shared between threads safely
2-
--> $DIR/issue-7364.rs:4:1
2+
--> $DIR/issue-7364.rs:4:15
33
|
44
LL | static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RefCell<isize>` cannot be shared between threads safely
5+
| ^^^^^^^^^^^^^^^^^^^ `RefCell<isize>` cannot be shared between threads safely
66
|
77
= help: the trait `Sync` is not implemented for `RefCell<isize>`
88
= note: if you want to do aliasing and mutation between multiple threads, use `std::sync::RwLock` instead

tests/ui/static/issue-24446.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
2-
--> $DIR/issue-24446.rs:2:5
2+
--> $DIR/issue-24446.rs:2:17
33
|
44
LL | static foo: dyn Fn() -> u32 = || -> u32 {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
5+
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
66
|
77
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
88
= note: shared static variables must have a type that implements `Sync`

tests/ui/statics/issue-17718-static-sync.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `Foo` cannot be shared between threads safely
2-
--> $DIR/issue-17718-static-sync.rs:9:1
2+
--> $DIR/issue-17718-static-sync.rs:9:13
33
|
44
LL | static BAR: Foo = Foo;
5-
| ^^^^^^^^^^^^^^^ `Foo` cannot be shared between threads safely
5+
| ^^^ `Foo` cannot be shared between threads safely
66
|
77
= help: the trait `Sync` is not implemented for `Foo`
88
= note: shared static variables must have a type that implements `Sync`

tests/ui/statics/unsizing-wfcheck-issue-127299.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ LL | fn bar() -> i32 where Self: Sized;
2222
| +++++++++++++++++
2323

2424
error[E0277]: `(dyn Qux + 'static)` cannot be shared between threads safely
25-
--> $DIR/unsizing-wfcheck-issue-127299.rs:12:1
25+
--> $DIR/unsizing-wfcheck-issue-127299.rs:12:13
2626
|
2727
LL | static FOO: &Lint = &Lint { desc: "desc" };
28-
| ^^^^^^^^^^^^^^^^^ `(dyn Qux + 'static)` cannot be shared between threads safely
28+
| ^^^^^ `(dyn Qux + 'static)` cannot be shared between threads safely
2929
|
3030
= help: within `&'static Lint`, the trait `Sync` is not implemented for `(dyn Qux + 'static)`
3131
= note: required because it appears within the type `&'static (dyn Qux + 'static)`

0 commit comments

Comments
 (0)