Skip to content

Commit 5c5e12f

Browse files
committed
Fix ICE when passing DefId-creating args to legacy_const_generics.
1 parent a5efa01 commit 5c5e12f

File tree

7 files changed

+103
-21
lines changed

7 files changed

+103
-21
lines changed

compiler/rustc_ast_lowering/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ ast_lowering_invalid_asm_template_modifier_reg_class =
103103
ast_lowering_invalid_asm_template_modifier_sym =
104104
asm template modifiers are not allowed for `sym` arguments
105105
106+
ast_lowering_invalid_legacy_const_generic_arg =
107+
invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
106108
ast_lowering_invalid_register =
107109
invalid register `{$reg}`: {$error}
108110

compiler/rustc_ast_lowering/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,10 @@ pub(crate) struct YieldInClosure {
451451
#[suggestion(code = "#[coroutine] ", applicability = "maybe-incorrect", style = "verbose")]
452452
pub suggestion: Option<Span>,
453453
}
454+
455+
#[derive(Diagnostic)]
456+
#[diag(ast_lowering_invalid_legacy_const_generic_arg)]
457+
pub(crate) struct InvalidLegacyConstGenericArg {
458+
#[primary_span]
459+
pub span: Span,
460+
}

compiler/rustc_ast_lowering/src/expr.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::assert_matches::assert_matches;
2+
use std::ops::ControlFlow;
23

34
use rustc_ast::ptr::P as AstP;
45
use rustc_ast::*;
@@ -12,6 +13,7 @@ use rustc_span::source_map::{respan, Spanned};
1213
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1314
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
1415
use thin_vec::{thin_vec, ThinVec};
16+
use visit::{walk_expr, Visitor};
1517

1618
use super::errors::{
1719
AsyncCoroutinesNotSupported, AwaitOnlyInAsyncFnAndBlocks, BaseExpressionDoubleDot,
@@ -22,9 +24,32 @@ use super::errors::{
2224
use super::{
2325
GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
2426
};
25-
use crate::errors::YieldInClosure;
27+
use crate::errors::{InvalidLegacyConstGenericArg, YieldInClosure};
2628
use crate::{fluent_generated, FnDeclKind, ImplTraitPosition};
2729

30+
struct WillCreateDefIdsVisitor {}
31+
32+
impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
33+
type Result = ControlFlow<Span>;
34+
35+
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
36+
ControlFlow::Break(c.value.span)
37+
}
38+
39+
fn visit_item(&mut self, item: &'v Item) -> Self::Result {
40+
ControlFlow::Break(item.span)
41+
}
42+
43+
fn visit_expr(&mut self, ex: &'v Expr) -> Self::Result {
44+
match ex.kind {
45+
ExprKind::Gen(..) | ExprKind::ConstBlock(..) | ExprKind::Closure(..) => {
46+
ControlFlow::Break(ex.span)
47+
}
48+
_ => walk_expr(self, ex),
49+
}
50+
}
51+
}
52+
2853
impl<'hir> LoweringContext<'_, 'hir> {
2954
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
3055
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
@@ -392,7 +417,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
392417
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
393418
}
394419

395-
let anon_const = AnonConst { id: node_id, value: arg };
420+
let mut visitor = WillCreateDefIdsVisitor {};
421+
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
422+
AstP(Expr {
423+
id: self.next_node_id(),
424+
kind: ExprKind::Err(
425+
self.tcx.dcx().emit_err(InvalidLegacyConstGenericArg { span }),
426+
),
427+
span: f.span,
428+
attrs: [].into(),
429+
tokens: None,
430+
})
431+
} else {
432+
arg
433+
};
434+
435+
let anon_const = AnonConst { id: node_id, value: const_value };
396436
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
397437
} else {
398438
real_args.push(arg);

tests/crashes/123077-2.rs

-12
This file was deleted.

tests/crashes/129150.rs

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fn foo<const U: i32>() -> i32 {
2+
U
3+
}
4+
5+
fn main() {
6+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, || ());
7+
//~^ invalid argument to a legacy const generic
8+
9+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, 5 + || ());
10+
//~^ invalid argument to a legacy const generic
11+
12+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<{ 1 + 2 }>());
13+
//~^ invalid argument to a legacy const generic
14+
15+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<3>());
16+
//~^ invalid argument to a legacy const generic
17+
18+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, &const {});
19+
//~^ invalid argument to a legacy const generic
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
2+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:6:55
3+
|
4+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, || ());
5+
| ^^^^^
6+
7+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
8+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:9:59
9+
|
10+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, 5 + || ());
11+
| ^^^^^
12+
13+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
14+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:12:61
15+
|
16+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<{ 1 + 2 }>());
17+
| ^^^^^^^^^
18+
19+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
20+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:15:61
21+
|
22+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<3>());
23+
| ^
24+
25+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
26+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:18:56
27+
|
28+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, &const {});
29+
| ^^^^^^^^
30+
31+
error: aborting due to 5 previous errors
32+

0 commit comments

Comments
 (0)