Skip to content

Commit 070d517

Browse files
committed
Fix ICE for invalid return activity and proper error handling
1 parent 6b29bb6 commit 070d517

File tree

7 files changed

+100
-49
lines changed

7 files changed

+100
-49
lines changed

Diff for: compiler/rustc_builtin_macros/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ builtin_macros_autodiff_mode = unknown Mode: `{$mode}`. Use `Forward` or `Revers
7575
builtin_macros_autodiff_mode_activity = {$act} can not be used in {$mode} Mode
7676
builtin_macros_autodiff_not_build = this rustc version does not support autodiff
7777
builtin_macros_autodiff_number_activities = expected {$expected} activities, but found {$found}
78+
builtin_macros_autodiff_ret_activity = invalid return activity {$act} in {$mode} Mode
7879
builtin_macros_autodiff_ty_activity = {$act} can not be used for this type
7980
81+
8082
builtin_macros_autodiff_unknown_activity = did not recognize Activity: `{$act}`
8183
builtin_macros_bad_derive_target = `derive` may only be applied to `struct`s, `enum`s and `union`s
8284
.label = not applicable here

Diff for: compiler/rustc_builtin_macros/src/autodiff.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ mod llvm_enzyme {
88
use std::string::String;
99

1010
use rustc_ast::expand::autodiff_attrs::{
11-
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ty_for_activity,
11+
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
12+
valid_ty_for_activity,
1213
};
1314
use rustc_ast::ptr::P;
1415
use rustc_ast::token::{Token, TokenKind};
@@ -632,10 +633,22 @@ mod llvm_enzyme {
632633
errors = true;
633634
}
634635
}
636+
637+
if has_ret && !valid_ret_activity(x.mode, x.ret_activity) {
638+
dcx.emit_err(errors::AutoDiffInvalidRetAct {
639+
span,
640+
mode: x.mode.to_string(),
641+
act: x.ret_activity.to_string(),
642+
});
643+
// We don't set `errors = true` to avoid annoying type errors relative
644+
// to the expanded macro type signature
645+
}
646+
635647
if errors {
636648
// This is not the right signature, but we can continue parsing.
637649
return (sig.clone(), new_inputs, idents, true);
638650
}
651+
639652
let unsafe_activities = x
640653
.input_activity
641654
.iter()

Diff for: compiler/rustc_builtin_macros/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ mod autodiff {
185185
pub(crate) act: String,
186186
}
187187

188+
#[derive(Diagnostic)]
189+
#[diag(builtin_macros_autodiff_ret_activity)]
190+
pub(crate) struct AutoDiffInvalidRetAct {
191+
#[primary_span]
192+
pub(crate) span: Span,
193+
pub(crate) mode: String,
194+
pub(crate) act: String,
195+
}
196+
188197
#[derive(Diagnostic)]
189198
#[diag(builtin_macros_autodiff_mode)]
190199
pub(crate) struct AutoDiffInvalidMode {

Diff for: compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::str::FromStr;
22

33
use rustc_abi::ExternAbi;
4-
use rustc_ast::expand::autodiff_attrs::{
5-
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
6-
};
4+
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
75
use rustc_ast::{MetaItem, MetaItemInner, attr};
86
use rustc_attr_parsing::ReprAttr::ReprAlign;
97
use rustc_attr_parsing::{AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr};
@@ -930,21 +928,6 @@ fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
930928
}
931929
}
932930

933-
// Validate input and return activities
934-
let mut msg = "".to_string();
935-
for &input in &arg_activities {
936-
if !valid_input_activity(mode, input) {
937-
msg = format!("Invalid input activity {} for {} mode", input, mode);
938-
}
939-
}
940-
if !valid_ret_activity(mode, ret_activity) {
941-
msg = format!("Invalid return activity {} for {} mode", ret_activity, mode);
942-
}
943-
if msg != "".to_string() {
944-
tcx.dcx().struct_span_err(attr.span(), msg).with_note("invalid activity").emit();
945-
return Some(AutoDiffAttrs::error());
946-
}
947-
948931
Some(AutoDiffAttrs { mode, ret_activity, input_activity: arg_activities })
949932
}
950933

Diff for: test_ad

560 KB
Binary file not shown.

Diff for: tests/ui/autodiff/autodiff_illegal.rs

+36-16
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,40 @@ use std::autodiff::autodiff;
1212
// We can't use Duplicated on scalars
1313
#[autodiff(df1, Reverse, Duplicated)]
1414
pub fn f1(x: f64) {
15-
//~^ ERROR Duplicated can not be used for this type
15+
//~^ ERROR Duplicated can not be used for this type
1616
unimplemented!()
1717
}
1818

1919
// Too many activities
2020
#[autodiff(df3, Reverse, Duplicated, Const)]
2121
pub fn f3(x: f64) {
22-
//~^^ ERROR expected 1 activities, but found 2
22+
//~^^ ERROR expected 1 activities, but found 2
2323
unimplemented!()
2424
}
2525

2626
// To few activities
2727
#[autodiff(df4, Reverse)]
2828
pub fn f4(x: f64) {
29-
//~^^ ERROR expected 1 activities, but found 0
29+
//~^^ ERROR expected 1 activities, but found 0
3030
unimplemented!()
3131
}
3232

3333
// We can't use Dual in Reverse mode
3434
#[autodiff(df5, Reverse, Dual)]
3535
pub fn f5(x: f64) {
36-
//~^^ ERROR Dual can not be used in Reverse Mode
36+
//~^^ ERROR Dual can not be used in Reverse Mode
3737
unimplemented!()
3838
}
3939

4040
// We can't use Duplicated in Forward mode
4141
#[autodiff(df6, Forward, Duplicated)]
4242
pub fn f6(x: f64) {
43-
//~^^ ERROR Duplicated can not be used in Forward Mode
44-
//~^^ ERROR Duplicated can not be used for this type
43+
//~^^ ERROR Duplicated can not be used in Forward Mode
44+
//~^^ ERROR Duplicated can not be used for this type
4545
unimplemented!()
4646
}
4747

4848
fn dummy() {
49-
5049
#[autodiff(df7, Forward, Dual)]
5150
let mut x = 5;
5251
//~^ ERROR autodiff must be applied to function
@@ -64,21 +63,21 @@ fn dummy() {
6463
// Malformed, where args?
6564
#[autodiff]
6665
pub fn f7(x: f64) {
67-
//~^ ERROR autodiff must be applied to function
66+
//~^ ERROR autodiff must be applied to function
6867
unimplemented!()
6968
}
7069

7170
// Malformed, where args?
7271
#[autodiff()]
7372
pub fn f8(x: f64) {
74-
//~^ ERROR autodiff requires at least a name and mode
73+
//~^ ERROR autodiff requires at least a name and mode
7574
unimplemented!()
7675
}
7776

7877
// Invalid attribute syntax
7978
#[autodiff = ""]
8079
pub fn f9(x: f64) {
81-
//~^ ERROR autodiff must be applied to function
80+
//~^ ERROR autodiff must be applied to function
8281
unimplemented!()
8382
}
8483

@@ -87,29 +86,29 @@ fn fn_exists() {}
8786
// We colide with an already existing function
8887
#[autodiff(fn_exists, Reverse, Active)]
8988
pub fn f10(x: f64) {
90-
//~^^ ERROR the name `fn_exists` is defined multiple times [E0428]
89+
//~^^ ERROR the name `fn_exists` is defined multiple times [E0428]
9190
unimplemented!()
9291
}
9392

9493
// Malformed, missing a mode
9594
#[autodiff(df11)]
9695
pub fn f11() {
97-
//~^ ERROR autodiff requires at least a name and mode
96+
//~^ ERROR autodiff requires at least a name and mode
9897
unimplemented!()
9998
}
10099

101100
// Invalid Mode
102101
#[autodiff(df12, Debug)]
103102
pub fn f12() {
104-
//~^^ ERROR unknown Mode: `Debug`. Use `Forward` or `Reverse`
103+
//~^^ ERROR unknown Mode: `Debug`. Use `Forward` or `Reverse`
105104
unimplemented!()
106105
}
107106

108107
// Invalid, please pick one Mode
109108
// or use two autodiff macros.
110109
#[autodiff(df13, Forward, Reverse)]
111110
pub fn f13() {
112-
//~^^ ERROR did not recognize Activity: `Reverse`
111+
//~^^ ERROR did not recognize Activity: `Reverse`
113112
unimplemented!()
114113
}
115114

@@ -130,7 +129,7 @@ type MyFloat = f32;
130129
// like THIR which has type information available.
131130
#[autodiff(df15, Reverse, Active, Active)]
132131
fn f15(x: MyFloat) -> f32 {
133-
//~^^ ERROR failed to resolve: use of undeclared type `MyFloat` [E0433]
132+
//~^^ ERROR failed to resolve: use of undeclared type `MyFloat` [E0433]
134133
unimplemented!()
135134
}
136135

@@ -141,7 +140,9 @@ fn f16(x: f32) -> MyFloat {
141140
}
142141

143142
#[repr(transparent)]
144-
struct F64Trans { inner: f64 }
143+
struct F64Trans {
144+
inner: f64,
145+
}
145146

146147
// We would like to support `#[repr(transparent)]` f32/f64 wrapper in return type in the future
147148
#[autodiff(df17, Reverse, Active, Active)]
@@ -156,5 +157,24 @@ fn f18(x: F64Trans) -> f64 {
156157
unimplemented!()
157158
}
158159

160+
// Invalid return activity
161+
#[autodiff(df19, Forward, Dual, Active)]
162+
fn f19(x: f32) -> f32 {
163+
//~^^ ERROR invalid return activity Active in Forward Mode
164+
unimplemented!()
165+
}
166+
167+
#[autodiff(df20, Reverse, Active, Dual)]
168+
fn f20(x: f32) -> f32 {
169+
//~^^ ERROR invalid return activity Dual in Reverse Mode
170+
unimplemented!()
171+
}
172+
173+
// Duplicated cannot be used as return activity
174+
#[autodiff(df21, Reverse, Active, Duplicated)]
175+
fn f21(x: f32) -> f32 {
176+
//~^^ ERROR invalid return activity Duplicated in Reverse Mode
177+
unimplemented!()
178+
}
159179

160180
fn main() {}

Diff for: tests/ui/autodiff/autodiff_illegal.stderr

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: attributes on expressions are experimental
2-
--> $DIR/autodiff_illegal.rs:54:5
2+
--> $DIR/autodiff_illegal.rs:53:5
33
|
44
LL | #[autodiff(df7, Forward, Dual)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -53,25 +53,25 @@ LL | pub fn f6(x: f64) {
5353
| ^^^
5454

5555
error: autodiff must be applied to function
56-
--> $DIR/autodiff_illegal.rs:51:5
56+
--> $DIR/autodiff_illegal.rs:50:5
5757
|
5858
LL | let mut x = 5;
5959
| ^^^^^^^^^^^^^^
6060

6161
error: autodiff must be applied to function
62-
--> $DIR/autodiff_illegal.rs:55:5
62+
--> $DIR/autodiff_illegal.rs:54:5
6363
|
6464
LL | x = x + 3;
6565
| ^
6666

6767
error: autodiff must be applied to function
68-
--> $DIR/autodiff_illegal.rs:60:5
68+
--> $DIR/autodiff_illegal.rs:59:5
6969
|
7070
LL | let add_one_v2 = |x: u32| -> u32 { x + 1 };
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7272

7373
error: autodiff must be applied to function
74-
--> $DIR/autodiff_illegal.rs:66:1
74+
--> $DIR/autodiff_illegal.rs:65:1
7575
|
7676
LL | / pub fn f7(x: f64) {
7777
LL | |
@@ -80,7 +80,7 @@ LL | | }
8080
| |_^
8181

8282
error: autodiff requires at least a name and mode
83-
--> $DIR/autodiff_illegal.rs:73:1
83+
--> $DIR/autodiff_illegal.rs:72:1
8484
|
8585
LL | / pub fn f8(x: f64) {
8686
LL | |
@@ -89,7 +89,7 @@ LL | | }
8989
| |_^
9090

9191
error: autodiff must be applied to function
92-
--> $DIR/autodiff_illegal.rs:80:1
92+
--> $DIR/autodiff_illegal.rs:79:1
9393
|
9494
LL | / pub fn f9(x: f64) {
9595
LL | |
@@ -98,7 +98,7 @@ LL | | }
9898
| |_^
9999

100100
error[E0428]: the name `fn_exists` is defined multiple times
101-
--> $DIR/autodiff_illegal.rs:88:1
101+
--> $DIR/autodiff_illegal.rs:87:1
102102
|
103103
LL | fn fn_exists() {}
104104
| -------------- previous definition of the value `fn_exists` here
@@ -110,7 +110,7 @@ LL | #[autodiff(fn_exists, Reverse, Active)]
110110
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
111111

112112
error: autodiff requires at least a name and mode
113-
--> $DIR/autodiff_illegal.rs:96:1
113+
--> $DIR/autodiff_illegal.rs:95:1
114114
|
115115
LL | / pub fn f11() {
116116
LL | |
@@ -119,34 +119,58 @@ LL | | }
119119
| |_^
120120

121121
error: unknown Mode: `Debug`. Use `Forward` or `Reverse`
122-
--> $DIR/autodiff_illegal.rs:102:18
122+
--> $DIR/autodiff_illegal.rs:101:18
123123
|
124124
LL | #[autodiff(df12, Debug)]
125125
| ^^^^^
126126

127127
error: did not recognize Activity: `Reverse`
128-
--> $DIR/autodiff_illegal.rs:110:27
128+
--> $DIR/autodiff_illegal.rs:109:27
129129
|
130130
LL | #[autodiff(df13, Forward, Reverse)]
131131
| ^^^^^^^
132132

133+
error: invalid return activity Active in Forward Mode
134+
--> $DIR/autodiff_illegal.rs:161:1
135+
|
136+
LL | #[autodiff(df19, Forward, Dual, Active)]
137+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138+
|
139+
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
140+
141+
error: invalid return activity Dual in Reverse Mode
142+
--> $DIR/autodiff_illegal.rs:167:1
143+
|
144+
LL | #[autodiff(df20, Reverse, Active, Dual)]
145+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146+
|
147+
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
148+
149+
error: invalid return activity Duplicated in Reverse Mode
150+
--> $DIR/autodiff_illegal.rs:174:1
151+
|
152+
LL | #[autodiff(df21, Reverse, Active, Duplicated)]
153+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154+
|
155+
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
156+
133157
error[E0433]: failed to resolve: use of undeclared type `MyFloat`
134-
--> $DIR/autodiff_illegal.rs:131:1
158+
--> $DIR/autodiff_illegal.rs:130:1
135159
|
136160
LL | #[autodiff(df15, Reverse, Active, Active)]
137161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `MyFloat`
138162
|
139163
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
140164

141165
error[E0433]: failed to resolve: use of undeclared type `F64Trans`
142-
--> $DIR/autodiff_illegal.rs:153:1
166+
--> $DIR/autodiff_illegal.rs:154:1
143167
|
144168
LL | #[autodiff(df18, Reverse, Active, Active)]
145169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `F64Trans`
146170
|
147171
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
148172

149-
error: aborting due to 19 previous errors
173+
error: aborting due to 22 previous errors
150174

151175
Some errors have detailed explanations: E0428, E0433, E0658.
152176
For more information about an error, try `rustc --explain E0428`.

0 commit comments

Comments
 (0)