Skip to content

Commit 51a29cb

Browse files
authored
Rollup merge of #83297 - oli-obk:why_bug_today_if_you_can_delay_to_tomorrow, r=petrochenkov
Do not ICE on ty::Error as an error must already have been reported fixes #83253
2 parents 767b094 + 9577058 commit 51a29cb

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

compiler/rustc_middle/src/ty/relate.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
1010
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
1111
use rustc_hir as ast;
1212
use rustc_hir::def_id::DefId;
13+
use rustc_span::DUMMY_SP;
1314
use rustc_target::spec::abi;
1415
use std::iter;
1516

@@ -499,11 +500,14 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
499500

500501
// FIXME(oli-obk): once const generics can have generic types, this assertion
501502
// will likely get triggered. Move to `normalize_erasing_regions` at that point.
502-
assert_eq!(
503-
tcx.erase_regions(a.ty),
504-
tcx.erase_regions(b.ty),
505-
"cannot relate constants of different types"
506-
);
503+
let a_ty = tcx.erase_regions(a.ty);
504+
let b_ty = tcx.erase_regions(b.ty);
505+
if a_ty != b_ty {
506+
relation.tcx().sess.delay_span_bug(
507+
DUMMY_SP,
508+
&format!("cannot relate constants of different types: {} != {}", a_ty, b_ty),
509+
);
510+
}
507511

508512
let eagerly_eval = |x: &'tcx ty::Const<'tcx>| x.eval(tcx, relation.param_env());
509513
let a = eagerly_eval(a);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo<const N: usize>() -> [u8; N] {
2+
bar::<N>() //~ ERROR mismatched types
3+
}
4+
5+
fn bar<const N: u8>() -> [u8; N] {}
6+
//~^ ERROR mismatched types
7+
//~| ERROR mismatched types
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/type_mismatch.rs:2:11
3+
|
4+
LL | bar::<N>()
5+
| ^ expected `u8`, found `usize`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/type_mismatch.rs:5:31
9+
|
10+
LL | fn bar<const N: u8>() -> [u8; N] {}
11+
| ^ expected `usize`, found `u8`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/type_mismatch.rs:5:26
15+
|
16+
LL | fn bar<const N: u8>() -> [u8; N] {}
17+
| --- ^^^^^^^ expected array `[u8; N]`, found `()`
18+
| |
19+
| implicitly returns `()` as its body has no tail or `return` expression
20+
21+
error: aborting due to 3 previous errors
22+
23+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl X {
2+
//~^ ERROR cannot find type
3+
fn getn<const N: usize>() -> [u8; N] {
4+
getn::<N>()
5+
}
6+
}
7+
fn getn<const N: cfg_attr>() -> [u8; N] {}
8+
//~^ ERROR expected type, found built-in attribute `cfg_attr`
9+
//~| ERROR mismatched types
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0412]: cannot find type `X` in this scope
2+
--> $DIR/type_not_in_scope.rs:1:6
3+
|
4+
LL | impl X {
5+
| ^ not found in this scope
6+
7+
error[E0573]: expected type, found built-in attribute `cfg_attr`
8+
--> $DIR/type_not_in_scope.rs:7:18
9+
|
10+
LL | fn getn<const N: cfg_attr>() -> [u8; N] {}
11+
| ^^^^^^^^ not a type
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/type_not_in_scope.rs:7:33
15+
|
16+
LL | fn getn<const N: cfg_attr>() -> [u8; N] {}
17+
| ---- ^^^^^^^ expected array `[u8; N]`, found `()`
18+
| |
19+
| implicitly returns `()` as its body has no tail or `return` expression
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors have detailed explanations: E0308, E0412, E0573.
24+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)