Skip to content

Commit e7abeb0

Browse files
authored
Rollup merge of rust-lang#119705 - fmease:tilde-const-assoc-fns-trait-impls, r=compiler-errors
Support `~const` in associated functions in trait impls Fixes rust-lang#119700.
2 parents 253efc9 + 3acc5a0 commit e7abeb0

File tree

4 files changed

+65
-30
lines changed

4 files changed

+65
-30
lines changed

compiler/rustc_ast_lowering/src/item.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
1313
use rustc_hir::PredicateOrigin;
1414
use rustc_index::{Idx, IndexSlice, IndexVec};
15+
use rustc_middle::span_bug;
1516
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1617
use rustc_span::edit_distance::find_best_match_for_name;
1718
use rustc_span::symbol::{kw, sym, Ident};
@@ -576,23 +577,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
576577
// This is used to track which lifetimes have already been defined,
577578
// and which need to be replicated when lowering an async fn.
578579

579-
match parent_hir.node().expect_item().kind {
580+
let generics = match parent_hir.node().expect_item().kind {
580581
hir::ItemKind::Impl(impl_) => {
581582
self.is_in_trait_impl = impl_.of_trait.is_some();
583+
&impl_.generics
582584
}
583-
hir::ItemKind::Trait(_, _, generics, _, _) if self.tcx.features().effects => {
584-
self.host_param_id = generics
585-
.params
586-
.iter()
587-
.find(|param| {
588-
matches!(
589-
param.kind,
590-
hir::GenericParamKind::Const { is_host_effect: true, .. }
591-
)
592-
})
593-
.map(|param| param.def_id);
585+
hir::ItemKind::Trait(_, _, generics, _, _) => generics,
586+
kind => {
587+
span_bug!(item.span, "assoc item has unexpected kind of parent: {}", kind.descr())
594588
}
595-
_ => {}
589+
};
590+
591+
if self.tcx.features().effects {
592+
self.host_param_id = generics
593+
.params
594+
.iter()
595+
.find(|param| {
596+
matches!(param.kind, hir::GenericParamKind::Const { is_host_effect: true, .. })
597+
})
598+
.map(|param| param.def_id);
596599
}
597600

598601
match ctxt {

tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-trait.stderr

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
error[E0277]: can't compare `impl PartialEq + Destruct + Copy` with `impl PartialEq + Destruct + Copy`
2-
--> $DIR/const-impl-trait.rs:28:17
1+
error[E0277]: can't compare `()` with `()`
2+
--> $DIR/const-impl-trait.rs:35:17
33
|
4-
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `impl PartialEq + Destruct + Copy == impl PartialEq + Destruct + Copy`
4+
LL | assert!(cmp(&()));
5+
| --- ^^^ no implementation for `() == ()`
6+
| |
7+
| required by a bound introduced by this call
68
|
7-
= help: the trait `~const PartialEq` is not implemented for `impl PartialEq + Destruct + Copy`
8-
note: required by a bound in `Foo::{opaque#0}`
9-
--> $DIR/const-impl-trait.rs:24:22
9+
= help: the trait `const PartialEq` is not implemented for `()`
10+
= help: the trait `PartialEq` is implemented for `()`
11+
note: required by a bound in `cmp`
12+
--> $DIR/const-impl-trait.rs:12:23
1013
|
11-
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
12-
| ^^^^^^^^^^^^^^^^ required by this bound in `Foo::{opaque#0}`
14+
LL | const fn cmp(a: &impl ~const PartialEq) -> bool {
15+
| ^^^^^^^^^^^^^^^^ required by this bound in `cmp`
1316

14-
error[E0277]: can't drop `impl PartialEq + Destruct + Copy`
15-
--> $DIR/const-impl-trait.rs:28:17
17+
error[E0277]: can't compare `&impl ~const PartialEq` with `&impl ~const PartialEq`
18+
--> $DIR/const-impl-trait.rs:13:7
1619
|
17-
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `impl PartialEq + Destruct + Copy`
20+
LL | a == a
21+
| ^^ no implementation for `&impl ~const PartialEq == &impl ~const PartialEq`
1922
|
20-
note: required by a bound in `Foo::{opaque#0}`
21-
--> $DIR/const-impl-trait.rs:24:41
23+
= help: the trait `~const PartialEq<&impl ~const PartialEq>` is not implemented for `&impl ~const PartialEq`
24+
help: consider dereferencing both sides of the expression
2225
|
23-
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
24-
| ^^^^^^^^^^^^^^^ required by this bound in `Foo::{opaque#0}`
26+
LL | *a == *a
27+
| + +
2528

2629
error: aborting due to 2 previous errors
2730

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Regression test for issue #119700.
2+
// check-pass
3+
4+
#![feature(const_trait_impl, effects)]
5+
6+
#[const_trait]
7+
trait Main {
8+
fn compute<T: ~const Aux>() -> u32;
9+
}
10+
11+
impl const Main for () {
12+
fn compute<T: ~const Aux>() -> u32 {
13+
T::generate()
14+
}
15+
}
16+
17+
#[const_trait]
18+
trait Aux {
19+
fn generate() -> u32;
20+
}
21+
22+
impl const Aux for () {
23+
fn generate() -> u32 { 1024 }
24+
}
25+
26+
fn main() {
27+
const _: u32 = <()>::compute::<()>();
28+
let _ = <()>::compute::<()>();
29+
}

0 commit comments

Comments
 (0)