Skip to content

Commit 72209e1

Browse files
authored
Unrolled build for rust-lang#121258
Rollup merge of rust-lang#121258 - fmease:assoc-const-eq-reject-overly-generic-tys, r=compiler-errors Reject overly generic assoc const binding types Split off from rust-lang#119385 to make rust-lang#119385 easier to review. --- In the *instantiated* type of assoc const bindings 1. reject **early-bound generic params** * Provide a rich error message instead of ICE'ing ([rust-lang#108271](rust-lang#108271)). * This is a temporary and semi-artificial restriction until the arrival of *generic const generics*. * It's quite possible that rustc could already perfectly support this subset of generic const generics if we just removed some checks (some `.no_bound_vars().expect(…)`) but even if that was the case, I'd rather gate it behind a new feature flag. Reporting an error instead of ICE'ing is a good first step towards an eventual feature gate error. 2. reject **escaping late-bound generic params** * They lead to ICEs before & I'm pretty sure that they remain incorrect even in a world with *generic const generics* --- Together with rust-lang#118668 & rust-lang#119385, this supersedes rust-lang#118360. Fixes rust-lang#108271.
2 parents d31b6fb + 0b2fb8d commit 72209e1

10 files changed

+463
-11
lines changed

compiler/rustc_hir_analysis/messages.ftl

+23
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ hir_analysis_enum_discriminant_overflowed = enum discriminant overflowed
118118
.label = overflowed on value after {$discr}
119119
.note = explicitly set `{$item_name} = {$wrapped_discr}` if that is desired outcome
120120
121+
hir_analysis_escaping_bound_var_in_ty_of_assoc_const_binding =
122+
the type of the associated constant `{$assoc_const}` cannot capture late-bound generic parameters
123+
.label = its type cannot capture the late-bound {$var_def_kind} `{$var_name}`
124+
.var_defined_here_label = the late-bound {$var_def_kind} `{$var_name}` is defined here
125+
121126
hir_analysis_field_already_declared =
122127
field `{$field_name}` is already declared
123128
.label = field already declared
@@ -316,6 +321,22 @@ hir_analysis_opaque_captures_higher_ranked_lifetime = `impl Trait` cannot captur
316321
.label = `impl Trait` implicitly captures all lifetimes in scope
317322
.note = lifetime declared here
318323
324+
hir_analysis_param_in_ty_of_assoc_const_binding =
325+
the type of the associated constant `{$assoc_const}` must not depend on {$param_category ->
326+
[self] `Self`
327+
[synthetic] `impl Trait`
328+
*[normal] generic parameters
329+
}
330+
.label = its type must not depend on {$param_category ->
331+
[self] `Self`
332+
[synthetic] `impl Trait`
333+
*[normal] the {$param_def_kind} `{$param_name}`
334+
}
335+
.param_defined_here_label = {$param_category ->
336+
[synthetic] the `impl Trait` is specified here
337+
*[normal] the {$param_def_kind} `{$param_name}` is defined here
338+
}
339+
319340
hir_analysis_paren_sugar_attribute = the `#[rustc_paren_sugar]` attribute is a temporary means of controlling which traits can use parenthetical notation
320341
.help = add `#![feature(unboxed_closures)]` to the crate attributes to use it
321342
@@ -432,6 +453,8 @@ hir_analysis_transparent_non_zero_sized_enum = the variant of a transparent {$de
432453
.label = needs at most one field with non-trivial size or alignment, but has {$field_count}
433454
.labels = this field has non-zero size or requires alignment
434455
456+
hir_analysis_ty_of_assoc_const_binding_note = `{$assoc_const}` has type `{$ty}`
457+
435458
hir_analysis_ty_param_first_local = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
436459
.label = type parameter `{$param_ty}` must be covered by another type when it appears before the first local type (`{$local_type}`)
437460
.note = implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+172-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use rustc_data_structures::fx::FxIndexMap;
1+
use std::ops::ControlFlow;
2+
3+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
24
use rustc_errors::{codes::*, struct_span_code_err};
35
use rustc_hir as hir;
46
use rustc_hir::def::{DefKind, Res};
57
use rustc_hir::def_id::{DefId, LocalDefId};
6-
use rustc_middle::ty::{self as ty, Ty};
8+
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TyCtxt};
79
use rustc_span::symbol::Ident;
8-
use rustc_span::{ErrorGuaranteed, Span};
10+
use rustc_span::{ErrorGuaranteed, Span, Symbol};
911
use rustc_trait_selection::traits;
12+
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
1013
use smallvec::SmallVec;
1114

1215
use crate::astconv::{AstConv, OnlySelfBounds, PredicateFilter};
@@ -433,14 +436,8 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
433436
binding.kind
434437
{
435438
let ty = alias_ty.map_bound(|ty| tcx.type_of(ty.def_id).instantiate(tcx, ty.args));
436-
// Since the arguments passed to the alias type above may contain early-bound
437-
// generic parameters, the instantiated type may contain some as well.
438-
// Therefore wrap it in `EarlyBinder`.
439-
// FIXME(fmease): Reject escaping late-bound vars.
440-
tcx.feed_anon_const_type(
441-
anon_const.def_id,
442-
ty::EarlyBinder::bind(ty.skip_binder()),
443-
);
439+
let ty = check_assoc_const_binding_type(tcx, assoc_ident, ty, binding.hir_id);
440+
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
444441
}
445442

446443
alias_ty
@@ -530,3 +527,167 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
530527
Ok(())
531528
}
532529
}
530+
531+
/// Detect and reject early-bound & escaping late-bound generic params in the type of assoc const bindings.
532+
///
533+
/// FIXME(const_generics): This is a temporary and semi-artifical restriction until the
534+
/// arrival of *generic const generics*[^1].
535+
///
536+
/// It might actually be possible that we can already support early-bound generic params
537+
/// in such types if we just lifted some more checks in other places, too, for example
538+
/// inside [`ty::Const::from_anon_const`]. However, even if that were the case, we should
539+
/// probably gate this behind another feature flag.
540+
///
541+
/// [^1]: <https://github.com/rust-lang/project-const-generics/issues/28>.
542+
fn check_assoc_const_binding_type<'tcx>(
543+
tcx: TyCtxt<'tcx>,
544+
assoc_const: Ident,
545+
ty: ty::Binder<'tcx, Ty<'tcx>>,
546+
hir_id: hir::HirId,
547+
) -> Ty<'tcx> {
548+
// We can't perform the checks for early-bound params during name resolution unlike E0770
549+
// because this information depends on *type* resolution.
550+
// We can't perform these checks in `resolve_bound_vars` either for the same reason.
551+
// Consider the trait ref `for<'a> Trait<'a, C = { &0 }>`. We need to know the fully
552+
// resolved type of `Trait::C` in order to know if it references `'a` or not.
553+
554+
let ty = ty.skip_binder();
555+
if !ty.has_param() && !ty.has_escaping_bound_vars() {
556+
return ty;
557+
}
558+
559+
let mut collector = GenericParamAndBoundVarCollector {
560+
tcx,
561+
params: Default::default(),
562+
vars: Default::default(),
563+
depth: ty::INNERMOST,
564+
};
565+
let mut guar = ty.visit_with(&mut collector).break_value();
566+
567+
let ty_note = ty
568+
.make_suggestable(tcx, false)
569+
.map(|ty| crate::errors::TyOfAssocConstBindingNote { assoc_const, ty });
570+
571+
let enclosing_item_owner_id = tcx
572+
.hir()
573+
.parent_owner_iter(hir_id)
574+
.find_map(|(owner_id, parent)| parent.generics().map(|_| owner_id))
575+
.unwrap();
576+
let generics = tcx.generics_of(enclosing_item_owner_id);
577+
for index in collector.params {
578+
let param = generics.param_at(index as _, tcx);
579+
let is_self_param = param.name == rustc_span::symbol::kw::SelfUpper;
580+
guar.get_or_insert(tcx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
581+
span: assoc_const.span,
582+
assoc_const,
583+
param_name: param.name,
584+
param_def_kind: tcx.def_descr(param.def_id),
585+
param_category: if is_self_param {
586+
"self"
587+
} else if param.kind.is_synthetic() {
588+
"synthetic"
589+
} else {
590+
"normal"
591+
},
592+
param_defined_here_label:
593+
(!is_self_param).then(|| tcx.def_ident_span(param.def_id).unwrap()),
594+
ty_note,
595+
}));
596+
}
597+
for (var_def_id, var_name) in collector.vars {
598+
guar.get_or_insert(tcx.dcx().emit_err(
599+
crate::errors::EscapingBoundVarInTyOfAssocConstBinding {
600+
span: assoc_const.span,
601+
assoc_const,
602+
var_name,
603+
var_def_kind: tcx.def_descr(var_def_id),
604+
var_defined_here_label: tcx.def_ident_span(var_def_id).unwrap(),
605+
ty_note,
606+
},
607+
));
608+
}
609+
610+
let guar = guar.unwrap_or_else(|| bug!("failed to find gen params or bound vars in ty"));
611+
Ty::new_error(tcx, guar)
612+
}
613+
614+
struct GenericParamAndBoundVarCollector<'tcx> {
615+
tcx: TyCtxt<'tcx>,
616+
params: FxIndexSet<u32>,
617+
vars: FxIndexSet<(DefId, Symbol)>,
618+
depth: ty::DebruijnIndex,
619+
}
620+
621+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx> {
622+
type Result = ControlFlow<ErrorGuaranteed>;
623+
624+
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
625+
&mut self,
626+
binder: &ty::Binder<'tcx, T>,
627+
) -> Self::Result {
628+
self.depth.shift_in(1);
629+
let result = binder.super_visit_with(self);
630+
self.depth.shift_out(1);
631+
result
632+
}
633+
634+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
635+
match ty.kind() {
636+
ty::Param(param) => {
637+
self.params.insert(param.index);
638+
}
639+
ty::Bound(db, bt) if *db >= self.depth => {
640+
self.vars.insert(match bt.kind {
641+
ty::BoundTyKind::Param(def_id, name) => (def_id, name),
642+
ty::BoundTyKind::Anon => {
643+
let reported = self
644+
.tcx
645+
.dcx()
646+
.delayed_bug(format!("unexpected anon bound ty: {:?}", bt.var));
647+
return ControlFlow::Break(reported);
648+
}
649+
});
650+
}
651+
_ if ty.has_param() || ty.has_bound_vars() => return ty.super_visit_with(self),
652+
_ => {}
653+
}
654+
ControlFlow::Continue(())
655+
}
656+
657+
fn visit_region(&mut self, re: ty::Region<'tcx>) -> Self::Result {
658+
match re.kind() {
659+
ty::ReEarlyParam(param) => {
660+
self.params.insert(param.index);
661+
}
662+
ty::ReBound(db, br) if db >= self.depth => {
663+
self.vars.insert(match br.kind {
664+
ty::BrNamed(def_id, name) => (def_id, name),
665+
ty::BrAnon | ty::BrEnv => {
666+
let guar = self
667+
.tcx
668+
.dcx()
669+
.delayed_bug(format!("unexpected bound region kind: {:?}", br.kind));
670+
return ControlFlow::Break(guar);
671+
}
672+
});
673+
}
674+
_ => {}
675+
}
676+
ControlFlow::Continue(())
677+
}
678+
679+
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
680+
match ct.kind() {
681+
ty::ConstKind::Param(param) => {
682+
self.params.insert(param.index);
683+
}
684+
ty::ConstKind::Bound(db, ty::BoundVar { .. }) if db >= self.depth => {
685+
let guar = self.tcx.dcx().delayed_bug("unexpected escaping late-bound const var");
686+
return ControlFlow::Break(guar);
687+
}
688+
_ if ct.has_param() || ct.has_bound_vars() => return ct.super_visit_with(self),
689+
_ => {}
690+
}
691+
ControlFlow::Continue(())
692+
}
693+
}

compiler/rustc_hir_analysis/src/errors.rs

+38
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,44 @@ pub struct AssocTypeBindingNotAllowed {
295295
pub fn_trait_expansion: Option<ParenthesizedFnTraitExpansion>,
296296
}
297297

298+
#[derive(Diagnostic)]
299+
#[diag(hir_analysis_param_in_ty_of_assoc_const_binding)]
300+
pub(crate) struct ParamInTyOfAssocConstBinding<'tcx> {
301+
#[primary_span]
302+
#[label]
303+
pub span: Span,
304+
pub assoc_const: Ident,
305+
pub param_name: Symbol,
306+
pub param_def_kind: &'static str,
307+
pub param_category: &'static str,
308+
#[label(hir_analysis_param_defined_here_label)]
309+
pub param_defined_here_label: Option<Span>,
310+
#[subdiagnostic]
311+
pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
312+
}
313+
314+
#[derive(Subdiagnostic, Clone, Copy)]
315+
#[note(hir_analysis_ty_of_assoc_const_binding_note)]
316+
pub(crate) struct TyOfAssocConstBindingNote<'tcx> {
317+
pub assoc_const: Ident,
318+
pub ty: Ty<'tcx>,
319+
}
320+
321+
#[derive(Diagnostic)]
322+
#[diag(hir_analysis_escaping_bound_var_in_ty_of_assoc_const_binding)]
323+
pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding<'tcx> {
324+
#[primary_span]
325+
#[label]
326+
pub span: Span,
327+
pub assoc_const: Ident,
328+
pub var_name: Symbol,
329+
pub var_def_kind: &'static str,
330+
#[label(hir_analysis_var_defined_here_label)]
331+
pub var_defined_here_label: Span,
332+
#[subdiagnostic]
333+
pub ty_note: Option<TyOfAssocConstBindingNote<'tcx>>,
334+
}
335+
298336
#[derive(Subdiagnostic)]
299337
#[help(hir_analysis_parenthesized_fn_trait_expansion)]
300338
pub struct ParenthesizedFnTraitExpansion {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Check that we eventually catch types of assoc const bounds
2+
// (containing late-bound vars) that are ill-formed.
3+
#![feature(associated_const_equality)]
4+
5+
trait Trait<T> {
6+
const K: T;
7+
}
8+
9+
fn take(
10+
_: impl Trait<
11+
<<for<'a> fn(&'a str) -> &'a str as Project>::Out as Discard>::Out,
12+
K = { () }
13+
>,
14+
) {}
15+
//~^^^^^^ ERROR implementation of `Project` is not general enough
16+
//~^^^^ ERROR higher-ranked subtype error
17+
//~| ERROR higher-ranked subtype error
18+
19+
trait Project { type Out; }
20+
impl<T> Project for fn(T) -> T { type Out = T; }
21+
22+
trait Discard { type Out; }
23+
impl<T: ?Sized> Discard for T { type Out = (); }
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: higher-ranked subtype error
2+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:12:13
3+
|
4+
LL | K = { () }
5+
| ^^^^^^
6+
7+
error: higher-ranked subtype error
8+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:12:13
9+
|
10+
LL | K = { () }
11+
| ^^^^^^
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
15+
error: implementation of `Project` is not general enough
16+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:9:4
17+
|
18+
LL | fn take(
19+
| ^^^^ implementation of `Project` is not general enough
20+
|
21+
= note: `Project` would have to be implemented for the type `for<'a> fn(&'a str) -> &'a str`
22+
= note: ...but `Project` is actually implemented for the type `fn(&'0 str) -> &'0 str`, for some specific lifetime `'0`
23+
24+
error: aborting due to 3 previous errors
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Check that we don't reject non-escaping late-bound vars in the type of assoc const bindings.
2+
// There's no reason why we should disallow them.
3+
//
4+
//@ check-pass
5+
6+
#![feature(associated_const_equality)]
7+
8+
trait Trait<T> {
9+
const K: T;
10+
}
11+
12+
fn take(
13+
_: impl Trait<
14+
<for<'a> fn(&'a str) -> &'a str as Discard>::Out,
15+
K = { () }
16+
>,
17+
) {}
18+
19+
trait Discard { type Out; }
20+
impl<T: ?Sized> Discard for T { type Out = (); }
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Detect and reject escaping late-bound generic params in
2+
// the type of assoc consts used in an equality bound.
3+
#![feature(associated_const_equality)]
4+
5+
trait Trait<'a> {
6+
const K: &'a ();
7+
}
8+
9+
fn take(_: impl for<'r> Trait<'r, K = { &() }>) {}
10+
//~^ ERROR the type of the associated constant `K` cannot capture late-bound generic parameters
11+
//~| NOTE its type cannot capture the late-bound lifetime parameter `'r`
12+
//~| NOTE the late-bound lifetime parameter `'r` is defined here
13+
//~| NOTE `K` has type `&'r ()`
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: the type of the associated constant `K` cannot capture late-bound generic parameters
2+
--> $DIR/assoc-const-eq-esc-bound-var-in-ty.rs:9:35
3+
|
4+
LL | fn take(_: impl for<'r> Trait<'r, K = { &() }>) {}
5+
| -- ^ its type cannot capture the late-bound lifetime parameter `'r`
6+
| |
7+
| the late-bound lifetime parameter `'r` is defined here
8+
|
9+
= note: `K` has type `&'r ()`
10+
11+
error: aborting due to 1 previous error
12+

0 commit comments

Comments
 (0)