Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a7e6391

Browse files
committed
mentioned_items: walk all types, not just consts
1 parent 33d2d93 commit a7e6391

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use std::ops::{Index, IndexMut};
4545
use std::{iter, mem};
4646

4747
pub use self::query::*;
48+
use self::visit::TyContext;
4849
pub use basic_blocks::BasicBlocks;
4950

5051
mod basic_blocks;
@@ -607,6 +608,17 @@ impl<'tcx> Body<'tcx> {
607608
}
608609
}
609610

611+
pub fn span_for_ty_context(&self, ty_context: TyContext) -> Span {
612+
match ty_context {
613+
TyContext::UserTy(span) => span,
614+
TyContext::ReturnTy(source_info)
615+
| TyContext::LocalDecl { source_info, .. }
616+
| TyContext::YieldTy(source_info)
617+
| TyContext::ResumeTy(source_info) => source_info.span,
618+
TyContext::Location(loc) => self.source_info(loc).span,
619+
}
620+
}
621+
610622
/// Returns the return type; it always return first element from `local_decls` array.
611623
#[inline]
612624
pub fn return_ty(&self) -> Ty<'tcx> {

compiler/rustc_mir_transform/src/mentioned_items.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_middle::mir::visit::Visitor;
2-
use rustc_middle::mir::{self, ConstOperand, Location, MentionedItem, MirPass};
2+
use rustc_middle::mir::{self, Location, MentionedItem, MirPass};
3+
use rustc_middle::ty::Ty;
34
use rustc_middle::ty::{self, adjustment::PointerCoercion, TyCtxt};
45
use rustc_session::Session;
56
use rustc_span::source_map::Spanned;
@@ -30,14 +31,13 @@ impl<'tcx> MirPass<'tcx> for MentionedItems {
3031
}
3132

3233
impl<'tcx> Visitor<'tcx> for MentionedItemsVisitor<'_, 'tcx> {
33-
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
34-
let const_ = constant.const_;
35-
// This is how function items get referenced: via constants of `FnDef` type. This handles
36-
// both functions that are called and those that are just turned to function pointers.
37-
if let ty::FnDef(def_id, args) = const_.ty().kind() {
34+
fn visit_ty(&mut self, ty: Ty<'tcx>, ty_context: mir::visit::TyContext) {
35+
if let ty::FnDef(def_id, args) = ty.kind() {
3836
debug!("adding to required_items: {def_id:?}");
39-
self.mentioned_items
40-
.push(Spanned { node: MentionedItem::Fn(*def_id, args), span: constant.span });
37+
self.mentioned_items.push(Spanned {
38+
node: MentionedItem::Fn(*def_id, args),
39+
span: self.body.span_for_ty_context(ty_context),
40+
});
4141
}
4242
}
4343

tests/ui/consts/required-consts/collect-in-dead-closure.opt.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ note: the above error was encountered while instantiating `fn not_called::<i32>`
1616
--> $DIR/collect-in-dead-closure.rs:23:33
1717
|
1818
LL | let _closure: fn() = || not_called::<T>();
19-
| ^^^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^^^^^
2020

2121
error: aborting due to 1 previous error
2222

tests/ui/consts/required-consts/collect-in-dead-fn.opt.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ note: the above error was encountered while instantiating `fn not_called::<i32>`
1616
--> $DIR/collect-in-dead-fn.rs:25:9
1717
|
1818
LL | not_called::<T>();
19-
| ^^^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^^^^^
2020

2121
error: aborting due to 1 previous error
2222

0 commit comments

Comments
 (0)