Skip to content

Commit

Permalink
Just totally fully deny late-bound consts
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jul 20, 2024
1 parent 2e6fc42 commit 0110538
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 30 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ ast_passes_fn_without_body =
ast_passes_forbidden_bound =
bounds cannot be used in this context
ast_passes_forbidden_const_param =
late-bound const parameters cannot be used currently
ast_passes_forbidden_default =
`default` is only allowed on items in trait impls
.label = `default` because of this
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ pub struct ForbiddenBound {
pub spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag(ast_passes_forbidden_const_param)]
pub struct ForbiddenConstParam {
#[primary_span]
pub const_param_spans: Vec<Span>,
}

#[derive(Diagnostic)]
#[diag(ast_passes_fn_param_too_many)]
pub struct FnParamTooMany {
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ impl<'a> PostExpansionVisitor<'a> {
crate::fluent_generated::ast_passes_forbidden_non_lifetime_param
);

// FIXME(non_lifetime_binders): Const bound params are super duper ultra hyper busted,
// and I'm frankly annoyed that people keep filing bugs about it even if it's marked
// as an incomplete feature.
if self.features.non_lifetime_binders {
let const_param_spans: Vec<_> = params
.iter()
.filter_map(|param| match param.kind {
ast::GenericParamKind::Const { .. } => Some(param.ident.span),
_ => None,
})
.collect();

if !const_param_spans.is_empty() {
self.sess.dcx().emit_err(errors::ForbiddenConstParam { const_param_spans });
}
}

for param in params {
if !param.bounds.is_empty() {
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2094,11 +2094,7 @@ pub fn deny_non_region_late_bound(
format!("late-bound {what} parameter not allowed on {where_}"),
);

let guar = if tcx.features().non_lifetime_binders && first {
diag.emit()
} else {
diag.delay_as_bug()
};
let guar = diag.emit_unless(!tcx.features().non_lifetime_binders || !first);

first = false;
*arg = ResolvedArg::Error(guar);
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2763,7 +2763,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
let res = match kind {
RibKind::Item(..) | RibKind::AssocItem => Res::Def(def_kind, def_id.to_def_id()),
RibKind::Normal => {
if self.r.tcx.features().non_lifetime_binders {
// FIXME(non_lifetime_binders): Stop special-casing
// const params to error out here.
if self.r.tcx.features().non_lifetime_binders
&& matches!(param.kind, GenericParamKind::Type { .. })
{
Res::Def(def_kind, def_id.to_def_id())
} else {
Res::Err
Expand Down
11 changes: 0 additions & 11 deletions tests/crashes/127009.rs

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/closures/binder/const-bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

fn main() {
for<const N: i32> || -> () {};
//~^ ERROR late-bound const parameter not allowed on closures
//~^ ERROR late-bound const parameters cannot be used currently
//~| ERROR late-bound const parameter not allowed on closures
}
8 changes: 7 additions & 1 deletion tests/ui/closures/binder/const-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error: late-bound const parameters cannot be used currently
--> $DIR/const-bound.rs:5:15
|
LL | for<const N: i32> || -> () {};
| ^

warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/const-bound.rs:1:37
|
Expand All @@ -13,5 +19,5 @@ error: late-bound const parameter not allowed on closures
LL | for<const N: i32> || -> () {};
| ^^^^^^^^^^^^

error: aborting due to 1 previous error; 1 warning emitted
error: aborting due to 2 previous errors; 1 warning emitted

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
pub fn foo()
where
for<const N: usize = { const fn bar() {} bar(); 1 }> ():,
//~^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^ ERROR late-bound const parameters cannot be used currently
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
{}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error: late-bound const parameters cannot be used currently
--> $DIR/no-entry-found-for-key-ice-gce-nlb-113133.rs:9:15
|
LL | for<const N: usize = { const fn bar() {} bar(); 1 }> ():,
| ^

error: defaults for generic parameters are not allowed in `for<...>` binders
--> $DIR/no-entry-found-for-key-ice-gce-nlb-113133.rs:9:9
|
LL | for<const N: usize = { const fn bar() {} bar(); 1 }> ():,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ trait TraitC {}
fn foo<T>()
where
for<const N: u8 = { T::A }> T: TraitA<AsA = impl TraitB<AsB = impl TraitC>>,
//~^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^ ERROR late-bound const parameters cannot be used currently
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~| ERROR `impl Trait` is not allowed in bounds
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error: late-bound const parameters cannot be used currently
--> $DIR/bad-suggestion-on-missing-assoc.rs:20:15
|
LL | for<const N: u8 = { T::A }> T: TraitA<AsA = impl TraitB<AsB = impl TraitC>>,
| ^

warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/bad-suggestion-on-missing-assoc.rs:1:12
|
Expand Down Expand Up @@ -29,6 +35,6 @@ LL | for<const N: u8 = { T::A }> T: TraitA<AsA = impl TraitB<AsB = impl Trai
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 2 previous errors; 2 warnings emitted
error: aborting due to 3 previous errors; 2 warnings emitted

For more information about this error, try `rustc --explain E0562`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
pub fn bar()
where
for<const N: usize = {
//~^ ERROR late-bound const parameters cannot be used currently
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
(||1usize)()
}> V: IntoIterator
//~^^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^^ ERROR cannot find type `V` in this scope
//~^ ERROR cannot find type `V` in this scope
{
}

Expand Down
12 changes: 10 additions & 2 deletions tests/ui/traits/non_lifetime_binders/binder-defaults-112547.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0412]: cannot find type `V` in this scope
--> $DIR/binder-defaults-112547.rs:8:4
--> $DIR/binder-defaults-112547.rs:10:4
|
LL | }> V: IntoIterator
| ^ not found in this scope
Expand All @@ -9,6 +9,12 @@ help: you might be missing a type parameter
LL | pub fn bar<V>()
| +++

error: late-bound const parameters cannot be used currently
--> $DIR/binder-defaults-112547.rs:6:15
|
LL | for<const N: usize = {
| ^

warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/binder-defaults-112547.rs:1:12
|
Expand All @@ -23,10 +29,12 @@ error: defaults for generic parameters are not allowed in `for<...>` binders
|
LL | for<const N: usize = {
| _________^
LL | |
LL | |
LL | | (||1usize)()
LL | | }> V: IntoIterator
| |_^

error: aborting due to 2 previous errors; 1 warning emitted
error: aborting due to 3 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0412`.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
fn fun()
where
for<T = (), const N: usize = 1> ():,
//~^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^ ERROR late-bound const parameters cannot be used currently
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
{}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error: late-bound const parameters cannot be used currently
--> $DIR/binder-defaults-119489.rs:7:23
|
LL | for<T = (), const N: usize = 1> ():,
| ^

warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/binder-defaults-119489.rs:1:12
|
Expand Down Expand Up @@ -27,5 +33,5 @@ error: defaults for generic parameters are not allowed in `for<...>` binders
LL | for<T = (), const N: usize = 1> ():,
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors; 2 warnings emitted
error: aborting due to 3 previous errors; 2 warnings emitted

11 changes: 11 additions & 0 deletions tests/ui/traits/non_lifetime_binders/late-const-param-wf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(non_lifetime_binders)]
//~^ WARN the feature `non_lifetime_binders` is incomplete

fn b()
where
for<const C: usize> [(); C]: Copy,
//~^ ERROR late-bound const parameters cannot be used currently
{
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/traits/non_lifetime_binders/late-const-param-wf.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: late-bound const parameters cannot be used currently
--> $DIR/late-const-param-wf.rs:6:15
|
LL | for<const C: usize> [(); C]: Copy,
| ^

warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/late-const-param-wf.rs:1:12
|
LL | #![feature(non_lifetime_binders)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

0 comments on commit 0110538

Please sign in to comment.