Skip to content

Commit 16ff824

Browse files
authored
Rollup merge of rust-lang#138284 - compiler-errors:const-param-ty-annotation, r=BoxyUwU
Do not write user type annotation for const param value path As I noted in the code comment, `DefKind::ConstParam` isn't actually *generic* over its own args, we just use the identity args from the body when lowering the value path so we have something to plug into the `EarlyBinder` we get back from `type_of` for the const param. So skip over it in `write_user_type_annotation_from_args`. Somewhat unrelated, but I left an explanation for a somewhat mysterious quirk in the THIR lowering of user type annotations for patterns having to do with ctors and their `type_of` not actually being the type of the pattern node it's ascribing. Fixes rust-lang#138048 r? ``@BoxyUwU``
2 parents 4ff58c9 + 8ab05ad commit 16ff824

File tree

7 files changed

+51
-11
lines changed

7 files changed

+51
-11
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220220
) {
221221
debug!("fcx {}", self.tag());
222222

223+
// Don't write user type annotations for const param types, since we give them
224+
// identity args just so that we can trivially substitute their `EarlyBinder`.
225+
// We enforce that they match their type in MIR later on.
226+
if matches!(self.tcx.def_kind(def_id), DefKind::ConstParam) {
227+
return;
228+
}
229+
223230
if Self::can_contain_user_lifetime_bounds((args, user_self_ty)) {
224231
let canonicalized = self.canonicalize_user_type_annotation(ty::UserType::new(
225232
ty::UserTypeKind::TypeOf(def_id, UserArgs { args, user_self_ty }),

Diff for: compiler/rustc_mir_build/src/thir/cx/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
207207
&self,
208208
hir_id: HirId,
209209
) -> Option<ty::CanonicalUserType<'tcx>> {
210-
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.typeck_results, hir_id)
210+
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.tcx, self.typeck_results, hir_id)
211211
}
212212
}
213213

Diff for: compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
539539
&self,
540540
hir_id: hir::HirId,
541541
) -> Option<ty::CanonicalUserType<'tcx>> {
542-
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.typeck_results, hir_id)
542+
crate::thir::util::user_args_applied_to_ty_of_hir_id(self.tcx, self.typeck_results, hir_id)
543543
}
544544

545545
/// Takes a HIR Path. If the path is a constant, evaluates it and feeds

Diff for: compiler/rustc_mir_build/src/thir/util.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use std::assert_matches::assert_matches;
2+
13
use rustc_hir as hir;
4+
use rustc_hir::def::DefKind;
25
use rustc_middle::bug;
3-
use rustc_middle::ty::{self, CanonicalUserType};
6+
use rustc_middle::ty::{self, CanonicalUserType, TyCtxt};
47
use tracing::debug;
58

69
/// Looks up the type associated with this hir-id and applies the
710
/// user-given generic parameters; the hir-id must map to a suitable
811
/// type.
912
pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
13+
tcx: TyCtxt<'tcx>,
1014
typeck_results: &ty::TypeckResults<'tcx>,
1115
hir_id: hir::HirId,
1216
) -> Option<CanonicalUserType<'tcx>> {
@@ -16,7 +20,23 @@ pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
1620
let ty = typeck_results.node_type(hir_id);
1721
match ty.kind() {
1822
ty::Adt(adt_def, ..) => {
23+
// This "fixes" user type annotations for tupled ctor patterns for ADTs.
24+
// That's because `type_of(ctor_did)` returns a FnDef, but we actually
25+
// want to be annotating the type of the ADT itself. It's a bit goofy,
26+
// but it's easier to adjust this here rather than in the path lowering
27+
// code for patterns in HIR.
1928
if let ty::UserTypeKind::TypeOf(did, _) = &mut user_ty.value.kind {
29+
// This is either already set up correctly (struct, union, enum, or variant),
30+
// or needs adjusting (ctor). Make sure we don't start adjusting other
31+
// user annotations like consts or fn calls.
32+
assert_matches!(
33+
tcx.def_kind(*did),
34+
DefKind::Ctor(..)
35+
| DefKind::Struct
36+
| DefKind::Enum
37+
| DefKind::Union
38+
| DefKind::Variant
39+
);
2040
*did = adt_def.did();
2141
}
2242
Some(user_ty)

Diff for: tests/crashes/138048.rs

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Foo<'a>(&'a ());
2+
3+
// We need a lifetime in scope or else we do not write a user type annotation as a fast-path.
4+
impl<'a> Foo<'a> {
5+
fn bar<const V: u8>() {
6+
let V;
7+
//~^ ERROR constant parameters cannot be referenced in patterns
8+
}
9+
}
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0158]: constant parameters cannot be referenced in patterns
2+
--> $DIR/bad-param-in-pat.rs:6:13
3+
|
4+
LL | fn bar<const V: u8>() {
5+
| ----------- constant defined here
6+
LL | let V;
7+
| ^ can't be used in patterns
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0158`.

0 commit comments

Comments
 (0)