Skip to content

Commit 7714a9a

Browse files
authored
Rollup merge of rust-lang#86750 - fee1-dead:impl-const-test, r=jonas-schievink
Test cross-crate usage of `feature(const_trait_impl)` This PR does two things: - Fixes metadata not encoded properly for functions in const trait impls. - Adds tests for using const trait impls cross-crate with the feature gate on the user crate either enabled or disabled. AFAIK, this means we can now constify some trait impls in the standard library 🎉 See rust-lang#67792 for the tracking issue, cc `@oli-obk`
2 parents f458d8f + c424510 commit 7714a9a

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,12 @@ impl EncodeContext<'a, 'tcx> {
12231223
let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind {
12241224
FnData {
12251225
asyncness: sig.header.asyncness,
1226-
constness: sig.header.constness,
1226+
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
1227+
constness: if self.tcx.is_const_fn_raw(def_id) {
1228+
hir::Constness::Const
1229+
} else {
1230+
hir::Constness::NotConst
1231+
},
12271232
param_names: self.encode_fn_param_names_for_body(body),
12281233
}
12291234
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(const_trait_impl)]
2+
#![allow(incomplete_features)]
3+
4+
pub trait MyTrait {
5+
fn func(self);
6+
}
7+
8+
pub struct NonConst;
9+
10+
impl MyTrait for NonConst {
11+
fn func(self) {
12+
13+
}
14+
}
15+
16+
pub struct Const;
17+
18+
impl const MyTrait for Const {
19+
fn func(self) {
20+
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// aux-build: cross-crate.rs
2+
extern crate cross_crate;
3+
4+
use cross_crate::*;
5+
6+
fn non_const_context() {
7+
NonConst.func();
8+
Const.func();
9+
}
10+
11+
const fn const_context() {
12+
NonConst.func();
13+
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants
14+
Const.func();
15+
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/cross-crate-feature-disabled.rs:12:5
3+
|
4+
LL | NonConst.func();
5+
| ^^^^^^^^^^^^^^^
6+
7+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8+
--> $DIR/cross-crate-feature-disabled.rs:14:5
9+
|
10+
LL | Const.func();
11+
| ^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(const_trait_impl)]
2+
#![allow(incomplete_features)]
3+
4+
// aux-build: cross-crate.rs
5+
extern crate cross_crate;
6+
7+
use cross_crate::*;
8+
9+
fn non_const_context() {
10+
NonConst.func();
11+
Const.func();
12+
}
13+
14+
const fn const_context() {
15+
NonConst.func();
16+
//~^ ERROR: calls in constant functions are limited to constant functions, tuple structs and tuple variants
17+
Const.func();
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/cross-crate-feature-enabled.rs:15:5
3+
|
4+
LL | NonConst.func();
5+
| ^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)