Skip to content

Commit a813cf9

Browse files
authored
Rollup merge of rust-lang#70284 - lcnr:issue70273-what-the-heck-git, r=eddyb
correctly handle const params in type_of extends rust-lang#70223, retry of rust-lang#70276 fixes rust-lang#70273 r? @eddyb cc @varkor
2 parents b306b1e + 0a17c4c commit a813cf9

File tree

7 files changed

+73
-15
lines changed

7 files changed

+73
-15
lines changed

src/librustc_hir/def.rs

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl DefKind {
159159
}
160160
}
161161

162+
/// The resolution of a path or export.
162163
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
163164
#[derive(HashStable_Generic)]
164165
pub enum Res<Id = hir::HirId> {

src/librustc_typeck/collect/type_of.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -235,38 +235,40 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
235235
};
236236

237237
if let Some(path) = path {
238-
let arg_index = path
238+
// We've encountered an `AnonConst` in some path, so we need to
239+
// figure out which generic parameter it corresponds to and return
240+
// the relevant type.
241+
242+
let (arg_index, segment) = path
239243
.segments
240244
.iter()
241-
.filter_map(|seg| seg.args.as_ref())
242-
.map(|generic_args| generic_args.args)
243-
.find_map(|args| {
245+
.filter_map(|seg| seg.args.as_ref().map(|args| (args.args, seg)))
246+
.find_map(|(args, seg)| {
244247
args.iter()
245248
.filter(|arg| arg.is_const())
246249
.enumerate()
247250
.filter(|(_, arg)| arg.id() == hir_id)
248-
.map(|(index, _)| index)
251+
.map(|(index, _)| (index, seg))
249252
.next()
250253
})
251254
.unwrap_or_else(|| {
252255
bug!("no arg matching AnonConst in path");
253256
});
254257

255-
// We've encountered an `AnonConst` in some path, so we need to
256-
// figure out which generic parameter it corresponds to and return
257-
// the relevant type.
258-
let generics = match path.res {
259-
Res::Def(DefKind::Ctor(..), def_id)
260-
| Res::Def(DefKind::AssocTy, def_id) => {
258+
// Try to use the segment resolution if it is valid, otherwise we
259+
// default to the path resolution.
260+
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
261+
let generics = match res {
262+
Res::Def(DefKind::Ctor(..), def_id) => {
261263
tcx.generics_of(tcx.parent(def_id).unwrap())
262264
}
263265
Res::Def(_, def_id) => tcx.generics_of(def_id),
264266
res => {
265267
tcx.sess.delay_span_bug(
266268
DUMMY_SP,
267269
&format!(
268-
"unexpected const parent path def, parent: {:?}, def: {:?}",
269-
parent_node, res
270+
"unexpected anon const res {:?} in path: {:?}",
271+
res, path,
270272
),
271273
);
272274
return tcx.types.err;
@@ -291,8 +293,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
291293
tcx.sess.delay_span_bug(
292294
DUMMY_SP,
293295
&format!(
294-
"missing generic parameter for `AnonConst`, parent {:?}",
295-
parent_node
296+
"missing generic parameter for `AnonConst`, parent: {:?}, res: {:?}",
297+
parent_node, res
296298
),
297299
);
298300
tcx.types.err

src/librustc_typeck/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
6262
#![feature(crate_visibility_modifier)]
6363
#![feature(in_band_lifetimes)]
6464
#![feature(nll)]
65+
#![feature(or_patterns)]
6566
#![feature(try_blocks)]
6667
#![feature(never_type)]
6768
#![feature(slice_partition_dedup)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
6+
trait T<const A: usize> {
7+
fn f();
8+
}
9+
struct S;
10+
11+
impl T<0usize> for S {
12+
fn f() {}
13+
}
14+
15+
fn main() {
16+
let _err = <S as T<0usize>>::f();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/issue70273-assoc-fn.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
6+
trait T<const A: usize> {
7+
fn l<const N: bool>() -> usize;
8+
fn r<const N: bool>() -> bool;
9+
}
10+
11+
struct S;
12+
13+
impl<const N: usize> T<N> for S {
14+
fn l<const M: bool>() -> usize { N }
15+
fn r<const M: bool>() -> bool { M }
16+
}
17+
18+
fn main() {
19+
assert_eq!(<S as T<123>>::l::<true>(), 123);
20+
assert!(<S as T<123>>::r::<true>());
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/type_of_anon_const.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

0 commit comments

Comments
 (0)