Skip to content

Commit 197368b

Browse files
committed
Emit fatal on defaults for generic params in binders if with nested defs
1 parent 4cf5f95 commit 197368b

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use super::{
2929
use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
3030
use crate::{AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, TryBlockScope};
3131

32-
struct WillCreateDefIdsVisitor {}
32+
pub(super) struct WillCreateDefIdsVisitor;
3333

3434
impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
3535
type Result = ControlFlow<Span>;
@@ -479,18 +479,18 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
479479
DefPathData::LateAnonConst,
480480
f.span,
481481
);
482-
let mut visitor = WillCreateDefIdsVisitor {};
483-
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
484-
Box::new(Expr {
485-
id: self.next_node_id(),
486-
kind: ExprKind::Err(invalid_expr_error(self.tcx, span)),
487-
span: f.span,
488-
attrs: [].into(),
489-
tokens: None,
490-
})
491-
} else {
492-
arg
493-
};
482+
let const_value =
483+
if let ControlFlow::Break(span) = WillCreateDefIdsVisitor.visit_expr(&arg) {
484+
Box::new(Expr {
485+
id: self.next_node_id(),
486+
kind: ExprKind::Err(invalid_expr_error(self.tcx, span)),
487+
span: f.span,
488+
attrs: [].into(),
489+
tokens: None,
490+
})
491+
} else {
492+
arg
493+
};
494494

495495
let anon_const = AnonConst {
496496
id: node_id,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::mem;
3939
use std::sync::Arc;
4040

4141
use rustc_ast::node_id::NodeMap;
42+
use rustc_ast::visit::Visitor;
4243
use rustc_ast::{self as ast, *};
4344
use rustc_attr_parsing::{AttributeParser, Late, OmitDoc};
4445
use rustc_data_structures::fingerprint::Fingerprint;
@@ -2219,14 +2220,19 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
22192220
// since later compiler stages cannot handle them (and shouldn't need to be able to).
22202221
let default = default
22212222
.as_ref()
2222-
.filter(|_| match source {
2223+
.filter(|anon_const| match source {
22232224
hir::GenericParamSource::Generics => true,
22242225
hir::GenericParamSource::Binder => {
2225-
self.dcx().emit_err(errors::GenericParamDefaultInBinder {
2226-
span: param.span(),
2227-
});
2228-
2229-
false
2226+
let err = errors::GenericParamDefaultInBinder { span: param.span() };
2227+
if expr::WillCreateDefIdsVisitor
2228+
.visit_expr(&anon_const.value)
2229+
.is_break()
2230+
{
2231+
self.dcx().emit_fatal(err)
2232+
} else {
2233+
self.dcx().emit_err(err);
2234+
false
2235+
}
22302236
}
22312237
})
22322238
.map(|def| self.lower_anon_const_to_const_arg_and_alloc(def));

tests/crashes/123629.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(generic_assert)]
2+
3+
fn foo()
4+
where
5+
for<const N: usize = { assert!(u) }> ():,
6+
//~^ ERROR cannot find value `u` in this scope
7+
//~^^ ERROR only lifetime parameters can be used in this context
8+
//~^^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
9+
{
10+
}
11+
12+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0425]: cannot find value `u` in this scope
2+
--> $DIR/bad-defaults-for-generic-param-in-binder-123629.rs:5:36
3+
|
4+
LL | for<const N: usize = { assert!(u) }> ():,
5+
| ^ not found in this scope
6+
7+
error[E0658]: only lifetime parameters can be used in this context
8+
--> $DIR/bad-defaults-for-generic-param-in-binder-123629.rs:5:15
9+
|
10+
LL | for<const N: usize = { assert!(u) }> ():,
11+
| ^
12+
|
13+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
14+
= help: add `#![feature(non_lifetime_binders)]` to the crate attributes to enable
15+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
16+
17+
error: defaults for generic parameters are not allowed in `for<...>` binders
18+
--> $DIR/bad-defaults-for-generic-param-in-binder-123629.rs:5:9
19+
|
20+
LL | for<const N: usize = { assert!(u) }> ():,
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
23+
error: aborting due to 3 previous errors
24+
25+
Some errors have detailed explanations: E0425, E0658.
26+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)