Skip to content

Commit 15cccb9

Browse files
Encode MIR for 'unreachable' non-generic fns
1 parent e40d5e8 commit 15cccb9

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

compiler/rustc_passes/src/reachable.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,15 @@ impl<'tcx> ReachableContext<'tcx> {
148148
hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
149149
| hir::TraitItemKind::Type(..) => false,
150150
},
151-
Some(Node::ImplItem(impl_item)) => {
152-
match impl_item.kind {
153-
hir::ImplItemKind::Const(..) => true,
154-
hir::ImplItemKind::Fn(..) => {
155-
let attrs = self.tcx.codegen_fn_attrs(def_id);
156-
let generics = self.tcx.generics_of(def_id);
157-
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
158-
true
159-
} else {
160-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
161-
let impl_did = self.tcx.hir().get_parent_item(hir_id);
162-
// Check the impl. If the generics on the self
163-
// type of the impl require inlining, this method
164-
// does too.
165-
match self.tcx.hir().expect_item(impl_did).kind {
166-
hir::ItemKind::Impl { .. } => {
167-
let generics = self.tcx.generics_of(impl_did);
168-
generics.requires_monomorphization(self.tcx)
169-
}
170-
_ => false,
171-
}
172-
}
173-
}
174-
hir::ImplItemKind::TyAlias(_) => false,
151+
Some(Node::ImplItem(impl_item)) => match impl_item.kind {
152+
hir::ImplItemKind::Const(..) => true,
153+
hir::ImplItemKind::Fn(..) => {
154+
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
155+
let impl_did = self.tcx.hir().get_parent_item(hir_id);
156+
method_might_be_inlined(self.tcx, impl_item, impl_did)
175157
}
176-
}
158+
hir::ImplItemKind::TyAlias(_) => false,
159+
},
177160
Some(_) => false,
178161
None => false, // This will happen for default methods.
179162
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{ptr::NonNull, task::Poll};
2+
3+
struct TaskRef;
4+
5+
struct Header {
6+
vtable: &'static Vtable,
7+
}
8+
9+
struct Vtable {
10+
poll: unsafe fn(TaskRef) -> Poll<()>,
11+
deallocate: unsafe fn(NonNull<Header>),
12+
}
13+
14+
// in the "Header" type, which is a private type in maitake
15+
impl Header {
16+
pub(crate) const fn new_stub() -> Self {
17+
unsafe fn nop(_ptr: TaskRef) -> Poll<()> {
18+
Poll::Pending
19+
}
20+
21+
unsafe fn nop_deallocate(ptr: NonNull<Header>) {
22+
unreachable!("stub task ({ptr:p}) should never be deallocated!");
23+
}
24+
25+
Self { vtable: &Vtable { poll: nop, deallocate: nop_deallocate } }
26+
}
27+
}
28+
29+
// This is a public type in `maitake`
30+
#[repr(transparent)]
31+
#[cfg_attr(loom, allow(dead_code))]
32+
pub struct TaskStub {
33+
hdr: Header,
34+
}
35+
36+
impl TaskStub {
37+
/// Create a new unique stub [`Task`].
38+
pub const fn new() -> Self {
39+
Self { hdr: Header::new_stub() }
40+
}
41+
}

src/test/ui/codegen/issue-97708.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-pass
2+
// aux-build:issue-97708-aux.rs
3+
4+
extern crate issue_97708_aux;
5+
use issue_97708_aux::TaskStub;
6+
7+
static TASK_STUB: TaskStub = TaskStub::new();
8+
9+
fn main() {}

0 commit comments

Comments
 (0)