From 5b88fbabebb2e3b2f04d8f67495bd9ea82661122 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 15 Aug 2019 13:56:57 +0900 Subject: [PATCH 1/4] Check FnHeader not to cause ICE --- src/librustc/middle/reachable.rs | 17 ++++++++++++++--- src/test/ui/issues/auxiliary/issue-63226.rs | 14 ++++++++++++++ src/test/ui/issues/issue-63226.rs | 10 ++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/issues/auxiliary/issue-63226.rs create mode 100644 src/test/ui/issues/issue-63226.rs diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index 76d8a6738f087..f9e9642f20ca3 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -32,9 +32,15 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item, attrs: CodegenFnAt return true } - match item.node { - hir::ItemKind::Impl(..) | - hir::ItemKind::Fn(..) => { + match item.node { + hir::ItemKind::Fn(_, header, ..) => { + if header.constness == hir::Constness::Const { + return true; + } + let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id)); + generics.requires_monomorphization(tcx) + } + hir::ItemKind::Impl(..) => { let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id)); generics.requires_monomorphization(tcx) } @@ -52,6 +58,11 @@ fn method_might_be_inlined( if codegen_fn_attrs.requests_inline() || generics.requires_monomorphization(tcx) { return true } + if let hir::ImplItemKind::Method(method_sig, _) = &impl_item.node { + if method_sig.header.constness == hir::Constness::Const { + return true + } + } if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) { match tcx.hir().find(impl_hir_id) { Some(Node::Item(item)) => diff --git a/src/test/ui/issues/auxiliary/issue-63226.rs b/src/test/ui/issues/auxiliary/issue-63226.rs new file mode 100644 index 0000000000000..39cc01a415e9a --- /dev/null +++ b/src/test/ui/issues/auxiliary/issue-63226.rs @@ -0,0 +1,14 @@ +pub struct VTable{ + state:extern fn(), +} + +impl VTable{ + pub const fn vtable()->&'static VTable{ + Self::VTABLE + } + + const VTABLE: &'static VTable = + &VTable{state}; +} + +extern fn state() {} diff --git a/src/test/ui/issues/issue-63226.rs b/src/test/ui/issues/issue-63226.rs new file mode 100644 index 0000000000000..f974bcf820c5b --- /dev/null +++ b/src/test/ui/issues/issue-63226.rs @@ -0,0 +1,10 @@ +// aux-build:issue-63226.rs +// compile-flags:--extern issue_63226 +// edition:2018 +// build-pass + +use issue_63226::VTable; + +static ICE_ICE:&'static VTable=VTable::vtable(); + +fn main() {} From 29b5ebf8a54a4751b04fc91d84c645dcc679da6f Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 15 Aug 2019 16:58:43 +0900 Subject: [PATCH 2/4] Use a match guard to avoid code repetition --- src/librustc/middle/reachable.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index f9e9642f20ca3..fd60978695c74 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -32,15 +32,12 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item, attrs: CodegenFnAt return true } - match item.node { - hir::ItemKind::Fn(_, header, ..) => { - if header.constness == hir::Constness::Const { - return true; - } - let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id)); - generics.requires_monomorphization(tcx) + match item.node { + hir::ItemKind::Fn(_, header, ..) if header.constness == hir::Constness::Const => { + return true; } - hir::ItemKind::Impl(..) => { + hir::ItemKind::Impl(..) | + hir::ItemKind::Fn(..) => { let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id)); generics.requires_monomorphization(tcx) } From e688352d2b6f4ad6fc931b84ff87b124433305af Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 15 Aug 2019 16:59:03 +0900 Subject: [PATCH 3/4] Adjust regression test --- src/test/ui/{issues => consts}/auxiliary/issue-63226.rs | 0 src/test/ui/{issues => consts}/issue-63226.rs | 2 ++ 2 files changed, 2 insertions(+) rename src/test/ui/{issues => consts}/auxiliary/issue-63226.rs (100%) rename src/test/ui/{issues => consts}/issue-63226.rs (68%) diff --git a/src/test/ui/issues/auxiliary/issue-63226.rs b/src/test/ui/consts/auxiliary/issue-63226.rs similarity index 100% rename from src/test/ui/issues/auxiliary/issue-63226.rs rename to src/test/ui/consts/auxiliary/issue-63226.rs diff --git a/src/test/ui/issues/issue-63226.rs b/src/test/ui/consts/issue-63226.rs similarity index 68% rename from src/test/ui/issues/issue-63226.rs rename to src/test/ui/consts/issue-63226.rs index f974bcf820c5b..deec44990086f 100644 --- a/src/test/ui/issues/issue-63226.rs +++ b/src/test/ui/consts/issue-63226.rs @@ -2,6 +2,8 @@ // compile-flags:--extern issue_63226 // edition:2018 // build-pass +// A regression test for issue #63226. +// Checks if `const fn` is marked as reachable. use issue_63226::VTable; From 7adb20e4cda94b9a726d99594afa506c6d40f453 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 15 Aug 2019 17:33:21 +0900 Subject: [PATCH 4/4] Use is_const() --- src/librustc/middle/reachable.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index fd60978695c74..c2bcd46216324 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -33,7 +33,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item, attrs: CodegenFnAt } match item.node { - hir::ItemKind::Fn(_, header, ..) if header.constness == hir::Constness::Const => { + hir::ItemKind::Fn(_, header, ..) if header.is_const() => { return true; } hir::ItemKind::Impl(..) | @@ -56,7 +56,7 @@ fn method_might_be_inlined( return true } if let hir::ImplItemKind::Method(method_sig, _) = &impl_item.node { - if method_sig.header.constness == hir::Constness::Const { + if method_sig.header.is_const() { return true } }