Skip to content

Commit 1d64a4b

Browse files
authored
Rollup merge of rust-lang#147642 - camelid:misc-const-fixes, r=BoxyUwU
Miscellaneous const-generics-related fixes Fixes rust-lang#129209. Fixes rust-lang#131295. Fixes rust-lang#139738.
2 parents 4a30664 + f39fb70 commit 1d64a4b

File tree

7 files changed

+36
-39
lines changed

7 files changed

+36
-39
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
7777
// stable enough and does not need a feature gate anymore.
7878
Node::AnonConst(_) => {
7979
let parent_did = tcx.parent(def_id.to_def_id());
80-
81-
// We don't do this unconditionally because the `DefId` parent of an anon const
82-
// might be an implicitly created closure during `async fn` desugaring. This would
83-
// have the wrong generics.
84-
//
85-
// i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
86-
// would implicitly have a closure in its body that would be the parent of
87-
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
88-
// instead of `['a]`.
89-
let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
90-
parent_did
91-
} else {
92-
tcx.hir_get_parent_item(hir_id).to_def_id()
93-
};
9480
debug!(?parent_did);
9581

9682
let mut in_param_ty = false;

compiler/rustc_mir_build/src/thir/constant.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ pub(crate) fn lit_to_const<'tcx>(
5858
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
5959
ty::ValTree::from_scalar_int(tcx, n.into())
6060
}
61-
(ast::LitKind::CStr(byte_sym, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) => {
62-
ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str())
61+
(ast::LitKind::CStr(byte_sym, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) =>
62+
{
63+
// A CStr is a newtype around a byte slice, so we create the inner slice here.
64+
// We need a branch for each "level" of the data structure.
65+
let bytes = ty::ValTree::from_raw_bytes(tcx, byte_sym.as_byte_str());
66+
ty::ValTree::from_branches(tcx, [bytes])
6367
}
6468
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
6569
let scalar_int = trunc(n.get(), *ui);

tests/crashes/129209.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/crashes/131295.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/crashes/139738.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
impl<const A: i32 = { || [0; B] }> Tr {}
2+
//~^ ERROR cannot find type `Tr`
3+
//~| ERROR cannot find value `B`
4+
//~| ERROR defaults for generic parameters are not allowed here
5+
6+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0412]: cannot find type `Tr` in this scope
2+
--> $DIR/parent_generics_of_nested_in_default.rs:1:36
3+
|
4+
LL | impl<const A: i32 = { || [0; B] }> Tr {}
5+
| ^^ not found in this scope
6+
7+
error[E0425]: cannot find value `B` in this scope
8+
--> $DIR/parent_generics_of_nested_in_default.rs:1:30
9+
|
10+
LL | impl<const A: i32 = { || [0; B] }> Tr {}
11+
| - ^ help: a const parameter with a similar name exists: `A`
12+
| |
13+
| similarly named const parameter `A` defined here
14+
15+
error: defaults for generic parameters are not allowed here
16+
--> $DIR/parent_generics_of_nested_in_default.rs:1:6
17+
|
18+
LL | impl<const A: i32 = { || [0; B] }> Tr {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors have detailed explanations: E0412, E0425.
24+
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)