Skip to content

Commit 44fb7f3

Browse files
committed
Fix ICE when passing DefId-creating args to legacy_const_generics.
1 parent 89ab655 commit 44fb7f3

File tree

7 files changed

+215
-22
lines changed

7 files changed

+215
-22
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ 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
108+
109+
ast_lowering_invalid_legacy_const_generic_arg_suggestion =
110+
try using a const generic parameter instead
111+
106112
ast_lowering_invalid_register =
107113
invalid register `{$reg}`: {$error}
108114

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,26 @@ 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+
#[subdiagnostic]
461+
pub suggestion: UseConstGenericArg,
462+
}
463+
464+
#[derive(Subdiagnostic)]
465+
#[multipart_suggestion(
466+
ast_lowering_invalid_legacy_const_generic_arg_suggestion,
467+
applicability = "maybe-incorrect"
468+
)]
469+
pub(crate) struct UseConstGenericArg {
470+
#[suggestion_part(code = "::<{const_args}>")]
471+
pub end_of_fn: Span,
472+
pub const_args: String,
473+
pub other_args: String,
474+
#[suggestion_part(code = "{other_args}")]
475+
pub call_args: Span,
476+
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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::*;
6+
use rustc_ast_pretty::pprust::expr_to_string;
57
use rustc_data_structures::stack::ensure_sufficient_stack;
68
use rustc_data_structures::sync::Lrc;
79
use rustc_hir as hir;
@@ -13,6 +15,7 @@ use rustc_span::source_map::{Spanned, respan};
1315
use rustc_span::symbol::{Ident, Symbol, kw, sym};
1416
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
1517
use thin_vec::{ThinVec, thin_vec};
18+
use visit::{Visitor, walk_expr};
1619

1720
use super::errors::{
1821
AsyncCoroutinesNotSupported, AwaitOnlyInAsyncFnAndBlocks, BaseExpressionDoubleDot,
@@ -23,9 +26,32 @@ use super::errors::{
2326
use super::{
2427
GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
2528
};
26-
use crate::errors::YieldInClosure;
29+
use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
2730
use crate::{AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, fluent_generated};
2831

32+
struct WillCreateDefIdsVisitor {}
33+
34+
impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
35+
type Result = ControlFlow<Span>;
36+
37+
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
38+
ControlFlow::Break(c.value.span)
39+
}
40+
41+
fn visit_item(&mut self, item: &'v Item) -> Self::Result {
42+
ControlFlow::Break(item.span)
43+
}
44+
45+
fn visit_expr(&mut self, ex: &'v Expr) -> Self::Result {
46+
match ex.kind {
47+
ExprKind::Gen(..) | ExprKind::ConstBlock(..) | ExprKind::Closure(..) => {
48+
ControlFlow::Break(ex.span)
49+
}
50+
_ => walk_expr(self, ex),
51+
}
52+
}
53+
}
54+
2955
impl<'hir> LoweringContext<'_, 'hir> {
3056
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
3157
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
@@ -399,7 +425,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
399425
// Split the arguments into const generics and normal arguments
400426
let mut real_args = vec![];
401427
let mut generic_args = ThinVec::new();
402-
for (idx, arg) in args.into_iter().enumerate() {
428+
for (idx, arg) in args.iter().cloned().enumerate() {
403429
if legacy_args_idx.contains(&idx) {
404430
let parent_def_id = self.current_def_id_parent;
405431
let node_id = self.next_node_id();
@@ -410,7 +436,39 @@ impl<'hir> LoweringContext<'_, 'hir> {
410436
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, f.span);
411437
}
412438

413-
let anon_const = AnonConst { id: node_id, value: arg };
439+
let mut visitor = WillCreateDefIdsVisitor {};
440+
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
441+
let mut const_args = vec![];
442+
let mut other_args = vec![];
443+
for (idx, arg) in args.iter().enumerate() {
444+
if legacy_args_idx.contains(&idx) {
445+
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
446+
} else {
447+
other_args.push(expr_to_string(arg));
448+
}
449+
}
450+
let suggestion = UseConstGenericArg {
451+
end_of_fn: f.span.shrink_to_hi(),
452+
const_args: const_args.join(", "),
453+
other_args: other_args.join(", "),
454+
call_args: args[0].span.to(args.last().unwrap().span),
455+
};
456+
AstP(Expr {
457+
id: self.next_node_id(),
458+
kind: ExprKind::Err(
459+
self.tcx
460+
.dcx()
461+
.emit_err(InvalidLegacyConstGenericArg { span, suggestion }),
462+
),
463+
span: f.span,
464+
attrs: [].into(),
465+
tokens: None,
466+
})
467+
} else {
468+
arg
469+
};
470+
471+
let anon_const = AnonConst { id: node_id, value: const_value };
414472
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
415473
} else {
416474
real_args.push(arg);

tests/crashes/123077-2.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/crashes/129150.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ only-x86_64
2+
3+
const fn foo<const U: i32>() -> i32 {
4+
U
5+
}
6+
7+
fn main() {
8+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, || ());
9+
//~^ invalid argument to a legacy const generic
10+
11+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, 5 + || ());
12+
//~^ invalid argument to a legacy const generic
13+
14+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<{ 1 + 2 }>());
15+
//~^ invalid argument to a legacy const generic
16+
17+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<3>());
18+
//~^ invalid argument to a legacy const generic
19+
20+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, &const {});
21+
//~^ invalid argument to a legacy const generic
22+
23+
std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, {
24+
struct F();
25+
//~^ invalid argument to a legacy const generic
26+
1
27+
});
28+
29+
std::arch::x86_64::_mm_inserti_si64(loop {}, loop {}, || (), 1 + || ());
30+
//~^ invalid argument to a legacy const generic
31+
//~^^ invalid argument to a legacy const generic
32+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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:8:55
3+
|
4+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, || ());
5+
| ^^^^^
6+
|
7+
help: try using a const generic parameter instead
8+
|
9+
LL | std::arch::x86_64::_mm_blend_ps::<{ || () }>(loop {}, loop {});
10+
| +++++++++++++ ~~~~~~~~~~~~~~~~
11+
12+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
13+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:11:59
14+
|
15+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, 5 + || ());
16+
| ^^^^^
17+
|
18+
help: try using a const generic parameter instead
19+
|
20+
LL | std::arch::x86_64::_mm_blend_ps::<{ 5 + (|| ()) }>(loop {}, loop {});
21+
| +++++++++++++++++++ ~~~~~~~~~~~~~~~~
22+
23+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
24+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:14:61
25+
|
26+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<{ 1 + 2 }>());
27+
| ^^^^^^^^^
28+
|
29+
help: try using a const generic parameter instead
30+
|
31+
LL | std::arch::x86_64::_mm_blend_ps::<{ foo::<{ 1 + 2 }>() }>(loop {}, loop {});
32+
| ++++++++++++++++++++++++++ ~~~~~~~~~~~~~~~~
33+
34+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
35+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:17:61
36+
|
37+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<3>());
38+
| ^
39+
|
40+
help: try using a const generic parameter instead
41+
|
42+
LL | std::arch::x86_64::_mm_blend_ps::<{ foo::<3>() }>(loop {}, loop {});
43+
| ++++++++++++++++++ ~~~~~~~~~~~~~~~~
44+
45+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
46+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:20:56
47+
|
48+
LL | std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, &const {});
49+
| ^^^^^^^^
50+
|
51+
help: try using a const generic parameter instead
52+
|
53+
LL | std::arch::x86_64::_mm_blend_ps::<{ &const {} }>(loop {}, loop {});
54+
| +++++++++++++++++ ~~~~~~~~~~~~~~~~
55+
56+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
57+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:24:9
58+
|
59+
LL | struct F();
60+
| ^^^^^^^^^^^
61+
|
62+
help: try using a const generic parameter instead
63+
|
64+
LL ~ std::arch::x86_64::_mm_blend_ps::<{ {
65+
LL + struct F();
66+
LL + 1
67+
LL ~ } }>(loop {}, loop {});
68+
|
69+
70+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
71+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:29:59
72+
|
73+
LL | std::arch::x86_64::_mm_inserti_si64(loop {}, loop {}, || (), 1 + || ());
74+
| ^^^^^
75+
|
76+
help: try using a const generic parameter instead
77+
|
78+
LL | std::arch::x86_64::_mm_inserti_si64::<{ || () }, { 1 + (|| ()) }>(loop {}, loop {});
79+
| ++++++++++++++++++++++++++++++ ~~~~~~~~~~~~~~~~
80+
81+
error: invalid argument to a legacy const generic: cannot have const blocks, closures, async blocks or items
82+
--> $DIR/invalid-rustc_legacy_const_generics-issue-123077.rs:29:70
83+
|
84+
LL | std::arch::x86_64::_mm_inserti_si64(loop {}, loop {}, || (), 1 + || ());
85+
| ^^^^^
86+
|
87+
help: try using a const generic parameter instead
88+
|
89+
LL | std::arch::x86_64::_mm_inserti_si64::<{ || () }, { 1 + (|| ()) }>(loop {}, loop {});
90+
| ++++++++++++++++++++++++++++++ ~~~~~~~~~~~~~~~~
91+
92+
error: aborting due to 8 previous errors
93+

0 commit comments

Comments
 (0)