From 3b9453bfe23a861ac1622f3562df7f1f980a4b34 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Wed, 30 Jun 2021 23:56:43 +0800 Subject: [PATCH 1/2] use is_const_fn_raw when encoding constness this properly encodes cross-crate constness data. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 7600739800041..4231db620b012 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1223,7 +1223,12 @@ impl EncodeContext<'a, 'tcx> { let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind { FnData { asyncness: sig.header.asyncness, - constness: sig.header.constness, + // Can be inside `impl const Trait`, so using sig.header.constness is not reliable + constness: if self.tcx.is_const_fn_raw(def_id) { + hir::Constness::Const + } else { + hir::Constness::NotConst + }, param_names: self.encode_fn_param_names_for_body(body), } } else { From c424510746c3e890157e3c30ad7fcf2051b9f769 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Wed, 30 Jun 2021 23:57:17 +0800 Subject: [PATCH 2/2] Add tests for cross-crate usage of `impl const` --- .../auxiliary/cross-crate.rs | 22 +++++++++++++++++++ .../cross-crate-feature-disabled.rs | 18 +++++++++++++++ .../cross-crate-feature-disabled.stderr | 15 +++++++++++++ .../cross-crate-feature-enabled.rs | 20 +++++++++++++++++ .../cross-crate-feature-enabled.stderr | 9 ++++++++ 5 files changed, 84 insertions(+) create mode 100644 src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs create mode 100644 src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.rs create mode 100644 src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.stderr create mode 100644 src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.rs create mode 100644 src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.stderr diff --git a/src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs b/src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs new file mode 100644 index 0000000000000..4285eaf18aaad --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs @@ -0,0 +1,22 @@ +#![feature(const_trait_impl)] +#![allow(incomplete_features)] + +pub trait MyTrait { + fn func(self); +} + +pub struct NonConst; + +impl MyTrait for NonConst { + fn func(self) { + + } +} + +pub struct Const; + +impl const MyTrait for Const { + fn func(self) { + + } +} diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.rs b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.rs new file mode 100644 index 0000000000000..abd11d8b0e923 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.rs @@ -0,0 +1,18 @@ +// aux-build: cross-crate.rs +extern crate cross_crate; + +use cross_crate::*; + +fn non_const_context() { + NonConst.func(); + Const.func(); +} + +const fn const_context() { + NonConst.func(); + //~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants + Const.func(); + //~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants +} + +fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.stderr b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.stderr new file mode 100644 index 0000000000000..b86583b9e079f --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-disabled.stderr @@ -0,0 +1,15 @@ +error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants + --> $DIR/cross-crate-feature-disabled.rs:12:5 + | +LL | NonConst.func(); + | ^^^^^^^^^^^^^^^ + +error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants + --> $DIR/cross-crate-feature-disabled.rs:14:5 + | +LL | Const.func(); + | ^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0015`. diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.rs b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.rs new file mode 100644 index 0000000000000..b79ccc7712ff2 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.rs @@ -0,0 +1,20 @@ +#![feature(const_trait_impl)] +#![allow(incomplete_features)] + +// aux-build: cross-crate.rs +extern crate cross_crate; + +use cross_crate::*; + +fn non_const_context() { + NonConst.func(); + Const.func(); +} + +const fn const_context() { + NonConst.func(); + //~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants + Const.func(); +} + +fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.stderr b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.stderr new file mode 100644 index 0000000000000..a544c0dd285fc --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/cross-crate-feature-enabled.stderr @@ -0,0 +1,9 @@ +error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants + --> $DIR/cross-crate-feature-enabled.rs:15:5 + | +LL | NonConst.func(); + | ^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0015`.