Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cross-crate usage of feature(const_trait_impl) #86750

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs
Original file line number Diff line number Diff line change
@@ -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) {

}
}
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`.