Skip to content

Commit 1d9ac3c

Browse files
committed
Fix const generics in GAT
1 parent 36931ce commit 1d9ac3c

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

Diff for: compiler/rustc_typeck/src/collect/type_of.rs

+42
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,48 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
2929
let parent_node = tcx.hir().get(parent_node_id);
3030

3131
match parent_node {
32+
Node::Ty(hir_ty @ Ty { kind: TyKind::Path(QPath::TypeRelative(_, segment)), .. }) => {
33+
let id = tcx
34+
.hir()
35+
.parent_iter(hir_id)
36+
.filter(|(_, node)| matches!(node, Node::Item(_)))
37+
.map(|(id, _)| id)
38+
.next()
39+
.unwrap();
40+
41+
let item_did = tcx.hir().local_def_id(id).to_def_id();
42+
let item_ctxt = &ItemCtxt::new(tcx, item_did) as &dyn crate::astconv::AstConv<'_>;
43+
let ty = item_ctxt.ast_ty_to_ty(hir_ty);
44+
45+
if let ty::Projection(projection) = ty.kind() {
46+
let generics = tcx.generics_of(projection.item_def_id);
47+
48+
let arg_index = segment
49+
.args
50+
.and_then(|args| {
51+
args.args
52+
.iter()
53+
.filter(|arg| arg.is_const())
54+
.position(|arg| arg.id() == hir_id)
55+
})
56+
.unwrap_or_else(|| {
57+
bug!("no arg matching AnonConst in segment");
58+
});
59+
60+
return generics
61+
.params
62+
.iter()
63+
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const))
64+
.nth(arg_index)
65+
.map(|param| param.def_id);
66+
}
67+
68+
tcx.sess.delay_span_bug(
69+
tcx.def_span(def_id),
70+
"unexpected non-GAT usage of an anon const",
71+
);
72+
return None;
73+
}
3274
Node::Expr(&Expr {
3375
kind:
3476
ExprKind::MethodCall(segment, ..) | ExprKind::Path(QPath::TypeRelative(_, segment)),

Diff for: compiler/rustc_typeck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This API is completely unstable and subject to change.
5656
*/
5757

5858
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
59+
#![feature(bindings_after_at)]
5960
#![feature(bool_to_option)]
6061
#![feature(box_syntax)]
6162
#![feature(crate_visibility_modifier)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-pass
2+
#![feature(generic_associated_types)]
3+
#![allow(incomplete_features)]
4+
5+
// This test unsures that with_opt_const_param returns the
6+
// def_id of the N param in the Foo::Assoc GAT.
7+
8+
trait Foo {
9+
type Assoc<const N: usize>;
10+
fn foo(&self) -> Self::Assoc<3>;
11+
}
12+
13+
impl Foo for () {
14+
type Assoc<const N: usize> = [(); N];
15+
fn foo(&self) -> Self::Assoc<3> {
16+
[(); 3]
17+
}
18+
}
19+
20+
fn main() {
21+
assert_eq!(().foo(), [(); 3]);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-pass
2+
#![feature(generic_associated_types)]
3+
#![allow(incomplete_features)]
4+
5+
// This test unsures that with_opt_const_param returns the
6+
// def_id of the N param in the Foo::Assoc GAT.
7+
8+
trait Foo {
9+
type Assoc<const N: usize>;
10+
fn foo<const N: usize>(&self) -> Self::Assoc<N>;
11+
}
12+
13+
impl Foo for () {
14+
type Assoc<const N: usize> = [(); N];
15+
fn foo<const N: usize>(&self) -> Self::Assoc<N> {
16+
[(); N]
17+
}
18+
}
19+
20+
fn main() {
21+
assert_eq!(().foo::<10>(), [(); 10]);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// run-pass
2+
#![feature(generic_associated_types)]
3+
#![allow(incomplete_features)]
4+
5+
// This test unsures that with_opt_const_param returns the
6+
// def_id of the N param in the Bar::Assoc GAT.
7+
8+
trait Bar {
9+
type Assoc<const N: usize>;
10+
}
11+
trait Foo: Bar {
12+
fn foo(&self) -> Self::Assoc<3>;
13+
}
14+
15+
impl Bar for () {
16+
type Assoc<const N: usize> = [(); N];
17+
}
18+
19+
impl Foo for () {
20+
fn foo(&self) -> Self::Assoc<3> {
21+
[(); 3]
22+
}
23+
}
24+
25+
fn main() {
26+
assert_eq!(().foo(), [(); 3]);
27+
}

0 commit comments

Comments
 (0)