Skip to content

Commit fa9f21b

Browse files
committed
address review comments
1 parent aed44b6 commit fa9f21b

File tree

8 files changed

+55
-10
lines changed

8 files changed

+55
-10
lines changed

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,9 @@ impl<'tcx> TyCtxt<'tcx> {
21812181
self.constness(def_id) == hir::Constness::Const
21822182
}
21832183
DefKind::Impl { of_trait: true } => {
2184-
let Some(trait_method_did) = self.trait_item_of(def_id) else { return false; };
2184+
let Some(trait_method_did) = self.trait_item_of(def_id) else {
2185+
return false;
2186+
};
21852187
self.constness(trait_method_did) == hir::Constness::Const
21862188
&& self.is_conditionally_const(parent_def_id)
21872189
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
- [Inference details](./opaque-types-impl-trait-inference.md)
199199
- [Return Position Impl Trait In Trait](./return-position-impl-trait-in-trait.md)
200200
- [Region inference restrictions][opaque-infer]
201-
- [Const condition checking](./effects.md)
201+
- [Const traits and const condition checking](./effects.md)
202202
- [Pattern and exhaustiveness checking](./pat-exhaustive-checking.md)
203203
- [Unsafety checking](./unsafety-checking.md)
204204
- [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/no-const-callers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ 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+
1319
const fn foo<T: [const] A>() {
1420
T::a();
1521
T::b();
1622
//~^ ERROR: cannot call non-const associated function
1723
<()>::a();
1824
<()>::b();
1925
//~^ ERROR: cannot call non-const associated function
26+
u8::a();
27+
u8::b();
28+
//~^ ERROR: cannot call non-const associated function
2029
}
2130

2231
fn main() {}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
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+
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
112
error[E0015]: cannot call non-const associated function `<T as A>::b` in constant functions
2-
--> $DIR/no-const-callers.rs:15:5
13+
--> $DIR/no-const-callers.rs:21:5
314
|
415
LL | T::b();
516
| ^^^^^^
617
|
718
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
819

920
error[E0015]: cannot call non-const associated function `<() as A>::b` in constant functions
10-
--> $DIR/no-const-callers.rs:18:5
21+
--> $DIR/no-const-callers.rs:24:5
1122
|
1223
LL | <()>::b();
1324
| ^^^^^^^^^
1425
|
1526
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
1627

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

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

0 commit comments

Comments
 (0)