Skip to content

Commit b58693f

Browse files
committed
Emit fatal on invalid const args with nested defs
1 parent f5eca4f commit b58693f

File tree

9 files changed

+76
-42
lines changed

9 files changed

+76
-42
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::mem;
3939
use std::sync::Arc;
4040

4141
use rustc_ast::node_id::NodeMap;
42-
use rustc_ast::visit::AssocCtxt;
42+
use rustc_ast::visit::{AssocCtxt, Visitor};
4343
use rustc_ast::{self as ast, *};
4444
use rustc_attr_parsing::{AttributeParser, Late, OmitDoc};
4545
use rustc_data_structures::fingerprint::Fingerprint;
@@ -2580,12 +2580,14 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
25802580
let span = self.lower_span(expr.span);
25812581

25822582
let overly_complex_const = |this: &mut Self| {
2583-
let e = this.dcx().struct_span_err(
2584-
expr.span,
2585-
"complex const arguments must be placed inside of a `const` block",
2586-
);
2583+
let msg = "complex const arguments must be placed inside of a `const` block";
2584+
let e = if expr::WillCreateDefIdsVisitor.visit_expr(expr).is_break() {
2585+
this.dcx().struct_span_fatal(expr.span, msg).emit()
2586+
} else {
2587+
this.dcx().struct_span_err(expr.span, msg).emit()
2588+
};
25872589

2588-
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(e.emit()), span }
2590+
ConstArg { hir_id: this.next_id(), kind: hir::ConstArgKind::Error(e), span }
25892591
};
25902592

25912593
match &expr.kind {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: complex const arguments must be placed inside of a `const` block
2+
--> $DIR/array-expr-complex.rs:11:28
3+
|
4+
LL | takes_array::<{ [1, 2, 1 + 2] }>();
5+
| ^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: complex const arguments must be placed inside of a `const` block
2+
--> $DIR/array-expr-complex.rs:14:21
3+
|
4+
LL | takes_array::<{ [X; 3] }>();
5+
| ^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: complex const arguments must be placed inside of a `const` block
2+
--> $DIR/array-expr-complex.rs:17:21
3+
|
4+
LL | takes_array::<{ [0; Y] }>();
5+
| ^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
//@ revisions: r1 r2 r3
2+
13
#![expect(incomplete_features)]
24
#![feature(min_generic_const_args, adt_const_params)]
35

46
fn takes_array<const A: [u32; 3]>() {}
57

68
fn generic_caller<const X: u32, const Y: usize>() {
79
// not supported yet
10+
#[cfg(r1)]
811
takes_array::<{ [1, 2, 1 + 2] }>();
9-
//~^ ERROR: complex const arguments must be placed inside of a `const` block
12+
//[r1]~^ ERROR: complex const arguments must be placed inside of a `const` block
13+
#[cfg(r2)]
1014
takes_array::<{ [X; 3] }>();
11-
//~^ ERROR: complex const arguments must be placed inside of a `const` block
15+
//[r2]~^ ERROR: complex const arguments must be placed inside of a `const` block
16+
#[cfg(r3)]
1217
takes_array::<{ [0; Y] }>();
13-
//~^ ERROR: complex const arguments must be placed inside of a `const` block
18+
//[r3]~^ ERROR: complex const arguments must be placed inside of a `const` block
1419
}
1520

1621
fn main() {}

tests/ui/const-generics/mgca/array-expr-complex.stderr

Lines changed: 0 additions & 20 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(min_generic_const_args)]
2+
3+
trait Iter<
4+
const FN: fn() = {
5+
|| { //~ ERROR complex const arguments must be placed inside of a `const` block
6+
use std::io::*;
7+
write!(_, "")
8+
}
9+
},
10+
>
11+
{
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: complex const arguments must be placed inside of a `const` block
2+
--> $DIR/bad-const-arg-fn-154539.rs:5:9
3+
|
4+
LL | / || {
5+
LL | | use std::io::*;
6+
LL | | write!(_, "")
7+
LL | | }
8+
| |_________^
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)