Skip to content

Commit f21c435

Browse files
authored
Rollup merge of #109364 - compiler-errors:gat-const-arg, r=BoxyUwU
Only expect a GAT const param for `type_of` of GAT const arg IDK why we were account for both `is_ty_or_const` instead of just for a const param, since we're computing the `type_of` a const param specifically. Fixes #109300
2 parents 3efecba + dbedf40 commit f21c435

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

+40-28
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
278278
}
279279
TraitItemKind::Const(ty, body_id) => body_id
280280
.and_then(|body_id| {
281-
is_suggestable_infer_ty(ty)
282-
.then(|| infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant",))
281+
is_suggestable_infer_ty(ty).then(|| {
282+
infer_placeholder_type(
283+
tcx, def_id, body_id, ty.span, item.ident, "constant",
284+
)
285+
})
283286
})
284287
.unwrap_or_else(|| icx.to_ty(ty)),
285288
TraitItemKind::Type(_, Some(ty)) => icx.to_ty(ty),
@@ -335,14 +338,15 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
335338
}
336339
}
337340
ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty),
338-
ItemKind::Impl(hir::Impl { self_ty, .. }) => {
339-
match self_ty.find_self_aliases() {
340-
spans if spans.len() > 0 => {
341-
let guar = tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: () });
342-
tcx.ty_error(guar)
343-
},
344-
_ => icx.to_ty(*self_ty),
341+
ItemKind::Impl(hir::Impl { self_ty, .. }) => match self_ty.find_self_aliases() {
342+
spans if spans.len() > 0 => {
343+
let guar = tcx.sess.emit_err(crate::errors::SelfInImplSelf {
344+
span: spans.into(),
345+
note: (),
346+
});
347+
tcx.ty_error(guar)
345348
}
349+
_ => icx.to_ty(*self_ty),
346350
},
347351
ItemKind::Fn(..) => {
348352
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
@@ -364,7 +368,10 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
364368
..
365369
}) => {
366370
if in_trait && !tcx.impl_defaultness(owner).has_value() {
367-
span_bug!(tcx.def_span(def_id), "tried to get type of this RPITIT with no definition");
371+
span_bug!(
372+
tcx.def_span(def_id),
373+
"tried to get type of this RPITIT with no definition"
374+
);
368375
}
369376
find_opaque_ty_constraints_for_rpit(tcx, def_id, owner)
370377
}
@@ -453,15 +460,12 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
453460
tcx.adt_def(tcx.hir().get_parent_item(hir_id)).repr().discr_type().to_ty(tcx)
454461
}
455462

456-
Node::TypeBinding(
457-
TypeBinding {
458-
hir_id: binding_id,
459-
kind: TypeBindingKind::Equality { term: Term::Const(e) },
460-
ident,
461-
..
462-
},
463-
) if let Node::TraitRef(trait_ref) =
464-
tcx.hir().get_parent(*binding_id)
463+
Node::TypeBinding(TypeBinding {
464+
hir_id: binding_id,
465+
kind: TypeBindingKind::Equality { term: Term::Const(e) },
466+
ident,
467+
..
468+
}) if let Node::TraitRef(trait_ref) = tcx.hir().get_parent(*binding_id)
465469
&& e.hir_id == hir_id =>
466470
{
467471
let Some(trait_def_id) = trait_ref.trait_def_id() else {
@@ -475,7 +479,9 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
475479
def_id.to_def_id(),
476480
);
477481
if let Some(assoc_item) = assoc_item {
478-
tcx.type_of(assoc_item.def_id).subst_identity()
482+
tcx.type_of(assoc_item.def_id)
483+
.no_bound_vars()
484+
.expect("const parameter types cannot be generic")
479485
} else {
480486
// FIXME(associated_const_equality): add a useful error message here.
481487
tcx.ty_error_with_message(
@@ -485,10 +491,13 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
485491
}
486492
}
487493

488-
Node::TypeBinding(
489-
TypeBinding { hir_id: binding_id, gen_args, kind, ident, .. },
490-
) if let Node::TraitRef(trait_ref) =
491-
tcx.hir().get_parent(*binding_id)
494+
Node::TypeBinding(TypeBinding {
495+
hir_id: binding_id,
496+
gen_args,
497+
kind,
498+
ident,
499+
..
500+
}) if let Node::TraitRef(trait_ref) = tcx.hir().get_parent(*binding_id)
492501
&& let Some((idx, _)) =
493502
gen_args.args.iter().enumerate().find(|(_, arg)| {
494503
if let GenericArg::Const(ct) = arg {
@@ -517,15 +526,18 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::EarlyBinder<Ty<'_>>
517526
},
518527
def_id.to_def_id(),
519528
);
520-
if let Some(param)
521-
= assoc_item.map(|item| &tcx.generics_of(item.def_id).params[idx]).filter(|param| param.kind.is_ty_or_const())
529+
if let Some(assoc_item) = assoc_item
530+
&& let param = &tcx.generics_of(assoc_item.def_id).params[idx]
531+
&& matches!(param.kind, ty::GenericParamDefKind::Const { .. })
522532
{
523-
tcx.type_of(param.def_id).subst_identity()
533+
tcx.type_of(param.def_id)
534+
.no_bound_vars()
535+
.expect("const parameter types cannot be generic")
524536
} else {
525537
// FIXME(associated_const_equality): add a useful error message here.
526538
tcx.ty_error_with_message(
527539
DUMMY_SP,
528-
"Could not find associated const on trait",
540+
"Could not find const param on associated item",
529541
)
530542
}
531543
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(generic_const_exprs)]
2+
//~^ WARN the feature `generic_const_exprs` is incomplete
3+
4+
trait B {
5+
type U<T>;
6+
}
7+
8+
fn f<T: B<U<1i32> = ()>>() {}
9+
//~^ ERROR constant provided when a type was expected
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/mismatched-gat-subst-kind.rs:1:12
3+
|
4+
LL | #![feature(generic_const_exprs)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0747]: constant provided when a type was expected
11+
--> $DIR/mismatched-gat-subst-kind.rs:8:13
12+
|
13+
LL | fn f<T: B<U<1i32> = ()>>() {}
14+
| ^^^^
15+
16+
error: aborting due to previous error; 1 warning emitted
17+
18+
For more information about this error, try `rustc --explain E0747`.

0 commit comments

Comments
 (0)