Skip to content

Commit 02e10b2

Browse files
committed
fix issues and ui tests, address reviews
1 parent 6147a3f commit 02e10b2

10 files changed

Lines changed: 72 additions & 25 deletions

File tree

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
774774

775775
// Attempting to call a trait method?
776776
if let Some(trait_did) = tcx.trait_of_assoc(callee) {
777-
// We can't determine the actual callee here, so we have to do different checks
778-
// than usual.
777+
// We can't determine the actual callee (the underlying impl of the trait) here, so we have
778+
// to do different checks than usual.
779779

780780
trace!("attempting to call a trait method");
781781
let is_const = tcx.constness(callee) == hir::Constness::Const;

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13331333
rustc_non_const_trait_method, AttributeType::Normal, template!(Word),
13341334
ErrorFollowing, EncodeCrossCrate::No,
13351335
"`#[rustc_non_const_trait_method]` should only used by the standard library to mark trait methods \
1336-
as non-const to allow large traits to easier transition to const"
1336+
as non-const to allow large traits an easier transition to const"
13371337
),
13381338

13391339
BuiltinAttribute {

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn compare_method_predicate_entailment<'tcx>(
218218
trait_m_predicates.instantiate_own(tcx, trait_to_impl_args).map(|(predicate, _)| predicate),
219219
);
220220

221-
let is_conditionally_const = tcx.is_conditionally_const(impl_def_id);
221+
let is_conditionally_const = tcx.is_conditionally_const(impl_m.def_id);
222222
if is_conditionally_const {
223223
// Augment the hybrid param-env with the const conditions
224224
// of the impl header and the trait method.

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,10 @@ impl<'tcx> TyCtxt<'tcx> {
20872087
self.constness(def_id) == hir::Constness::Const
20882088
}
20892089
DefKind::Impl { of_trait: true } => {
2090-
self.constness(self.trait_item_of(def_id).unwrap()) == hir::Constness::Const
2090+
let Some(trait_method_did) = self.trait_item_of(def_id) else {
2091+
return false;
2092+
};
2093+
self.constness(trait_method_did) == hir::Constness::Const
20912094
&& self.is_conditionally_const(parent_def_id)
20922095
}
20932096
DefKind::Trait => {

src/doc/rustc-dev-guide/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
- [Inference details](./opaque-types-impl-trait-inference.md)
200200
- [Return Position Impl Trait In Trait](./return-position-impl-trait-in-trait.md)
201201
- [Region inference restrictions](./borrow-check/opaque-types-region-inference-restrictions.md)
202-
- [Const condition checking](./effects.md)
202+
- [Const traits and const condition checking](./effects.md)
203203
- [Pattern and exhaustiveness checking](./pat-exhaustive-checking.md)
204204
- [Unsafety checking](./unsafety-checking.md)
205205
- [MIR dataflow](./mir/dataflow.md)

src/doc/rustc-dev-guide/src/effects.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Effects and const condition checking
1+
# Effects, const traits, and const condition checking
22

33
## The `HostEffect` predicate
44

@@ -154,3 +154,18 @@ be dropped at compile time.
154154

155155
[old solver]: https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_trait_selection/traits/effects.rs.html
156156
[new trait solver]: https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_next_trait_solver/solve/effect_goals.rs.html
157+
158+
## More on const traits
159+
160+
To be expanded later.
161+
162+
### The `#[rustc_non_const_trait_method]` attribute
163+
164+
This is intended for internal (standard library) usage only. With this attribute
165+
applied to a trait method, the compiler will not check the default body of this
166+
method for ability to run in compile time. Users of the trait will also not be
167+
allowed to use this trait method in const contexts. This attribute is primarily
168+
used for constifying large traits such as `Iterator` without having to make all
169+
its methods `const` at the same time.
170+
171+
This attribute should not be present while stabilizing the trait as `const`.

tests/ui/traits/const-traits/partial/attr-gate.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #[rustc_non_const_trait_method]
66
|
77
= help: add `#![feature(rustc_attrs)]` to the crate attributes to enable
88
= note: the `#[rustc_non_const_trait_method]` attribute is an internal implementation detail that will never be stable
9-
= note: `#[rustc_non_const_trait_method]` should only used by the standard library to mark trait methods as non-const to allow large traits to easier transition to const
9+
= note: `#[rustc_non_const_trait_method]` should only used by the standard library to mark trait methods as non-const to allow large traits an easier transition to const
1010

1111
error: aborting due to 1 previous error
1212

tests/ui/traits/const-traits/partial/no-const-callers.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,30 @@ impl const A for () {
1010
fn a() {}
1111
}
1212

13+
impl const A for u8 {
14+
fn a() {}
15+
fn b() { println!("hello"); }
16+
//~^ ERROR: cannot call non-const function
17+
}
18+
19+
impl const A for i8 {
20+
fn a() {}
21+
fn b() {}
22+
}
23+
1324
const fn foo<T: [const] A>() {
1425
T::a();
1526
T::b();
1627
//~^ ERROR: cannot call non-const associated function
1728
<()>::a();
1829
<()>::b();
1930
//~^ ERROR: cannot call non-const associated function
31+
u8::a();
32+
u8::b();
33+
//~^ ERROR: cannot call non-const associated function
34+
i8::a();
35+
i8::b();
36+
//~^ ERROR: cannot call non-const associated function
2037
}
2138

2239
fn main() {}
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1+
error[E0015]: cannot call non-const function `std::io::_print` in constant functions
2+
--> $DIR/no-const-callers.rs:15:14
3+
|
4+
LL | fn b() { println!("hello"); }
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
note: function `_print` is not const
8+
--> $SRC_DIR/std/src/io/stdio.rs:LL:COL
9+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
10+
111
error[E0015]: cannot call non-const associated function `<T as A>::b` in constant functions
2-
--> $DIR/no-const-callers.rs:15:5
12+
--> $DIR/no-const-callers.rs:26:5
313
|
414
LL | T::b();
515
| ^^^^^^
616
|
717
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
818

919
error[E0015]: cannot call non-const associated function `<() as A>::b` in constant functions
10-
--> $DIR/no-const-callers.rs:18:5
20+
--> $DIR/no-const-callers.rs:29:5
1121
|
1222
LL | <()>::b();
1323
| ^^^^^^^^^
1424
|
1525
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1626

17-
error: aborting due to 2 previous errors
27+
error[E0015]: cannot call non-const associated function `<u8 as A>::b` in constant functions
28+
--> $DIR/no-const-callers.rs:32:5
29+
|
30+
LL | u8::b();
31+
| ^^^^^^^
32+
|
33+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
34+
35+
error[E0015]: cannot call non-const associated function `<i8 as A>::b` in constant functions
36+
--> $DIR/no-const-callers.rs:35:5
37+
|
38+
LL | i8::b();
39+
| ^^^^^^^
40+
|
41+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
42+
43+
error: aborting due to 5 previous errors
1844

1945
For more information about this error, try `rustc --explain E0015`.

tests/ui/typeck/typeck_type_placeholder_item.stderr

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -684,13 +684,6 @@ error[E0015]: cannot call non-const method `<std::ops::Range<i32> as Iterator>::
684684
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
685685
| ^^^^^^^^^^^^^^^^^^^^^^
686686
|
687-
note: method `filter` is not const because trait `Iterator` is not const
688-
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
689-
|
690-
= note: this trait is not const
691-
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
692-
|
693-
= note: this method is not const
694687
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
695688

696689
error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}> as Iterator>::map::<i32, {closure@$DIR/typeck_type_placeholder_item.rs:240:49: 240:52}>` in constants
@@ -699,13 +692,6 @@ error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closu
699692
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
700693
| ^^^^^^^^^^^^^^
701694
|
702-
note: method `map` is not const because trait `Iterator` is not const
703-
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
704-
|
705-
= note: this trait is not const
706-
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
707-
|
708-
= note: this method is not const
709695
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
710696

711697
error: aborting due to 83 previous errors

0 commit comments

Comments
 (0)