Skip to content

Commit dee4857

Browse files
committed
Graceful error for wrong number of activities
Added graceful error message instead of an ICE Blessed tests
1 parent 87e60a7 commit dee4857

File tree

3 files changed

+58
-49
lines changed

3 files changed

+58
-49
lines changed

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,9 @@ mod llvm_enzyme {
501501
};
502502

503503
if x.mode.is_fwd() && x.ret_activity == DiffActivity::Dual {
504-
assert!(d_ret_ty.len() == 2);
504+
if d_ret_ty.len() != 2 {
505+
return body;
506+
}
505507
// both should be identical, by construction
506508
let arg = d_ret_ty[0].kind.is_simple_path().unwrap();
507509
let arg2 = d_ret_ty[1].kind.is_simple_path().unwrap();

tests/ui/autodiff/autodiff_illegal.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ pub fn f4(x: f64) {
3030
unimplemented!()
3131
}
3232

33+
#[autodiff(df5, Forward, Dual, Dual)]
34+
fn f5(x: f32, y: f32) -> f32 {
35+
// ~^^ error: expected 3 activities, but found 2
36+
x+y
37+
}
38+
3339
// We can't use Dual in Reverse mode
34-
#[autodiff(df5, Reverse, Dual)]
35-
pub fn f5(x: f64) {
40+
#[autodiff(df6, Reverse, Dual)]
41+
pub fn f6(x: f64) {
3642
//~^^ ERROR Dual can not be used in Reverse Mode
3743
unimplemented!()
3844
}
3945

4046
// We can't use Duplicated in Forward mode
41-
#[autodiff(df6, Forward, Duplicated)]
42-
pub fn f6(x: f64) {
47+
#[autodiff(df7, Forward, Duplicated)]
48+
pub fn f7(x: f64) {
4349
//~^^ ERROR Duplicated can not be used in Forward Mode
4450
//~^^ ERROR Duplicated can not be used for this type
4551
unimplemented!()
@@ -62,21 +68,21 @@ fn dummy() {
6268

6369
// Malformed, where args?
6470
#[autodiff]
65-
pub fn f7(x: f64) {
71+
pub fn f8(x: f64) {
6672
//~^ ERROR autodiff must be applied to function
6773
unimplemented!()
6874
}
6975

7076
// Malformed, where args?
7177
#[autodiff()]
72-
pub fn f8(x: f64) {
78+
pub fn f9(x: f64) {
7379
//~^ ERROR autodiff requires at least a name and mode
7480
unimplemented!()
7581
}
7682

7783
// Invalid attribute syntax
7884
#[autodiff = ""]
79-
pub fn f9(x: f64) {
85+
pub fn f10(x: f64) {
8086
//~^ ERROR autodiff must be applied to function
8187
unimplemented!()
8288
}
@@ -85,29 +91,29 @@ fn fn_exists() {}
8591

8692
// We colide with an already existing function
8793
#[autodiff(fn_exists, Reverse, Active)]
88-
pub fn f10(x: f64) {
94+
pub fn f11(x: f64) {
8995
//~^^ ERROR the name `fn_exists` is defined multiple times [E0428]
9096
unimplemented!()
9197
}
9298

9399
// Malformed, missing a mode
94-
#[autodiff(df11)]
95-
pub fn f11() {
100+
#[autodiff(df12)]
101+
pub fn f12() {
96102
//~^ ERROR autodiff requires at least a name and mode
97103
unimplemented!()
98104
}
99105

100106
// Invalid Mode
101-
#[autodiff(df12, Debug)]
102-
pub fn f12() {
107+
#[autodiff(df13, Debug)]
108+
pub fn f13() {
103109
//~^^ ERROR unknown Mode: `Debug`. Use `Forward` or `Reverse`
104110
unimplemented!()
105111
}
106112

107113
// Invalid, please pick one Mode
108114
// or use two autodiff macros.
109-
#[autodiff(df13, Forward, Reverse)]
110-
pub fn f13() {
115+
#[autodiff(df14, Forward, Reverse)]
116+
pub fn f14() {
111117
//~^^ ERROR did not recognize Activity: `Reverse`
112118
unimplemented!()
113119
}
@@ -117,8 +123,8 @@ struct Foo {}
117123
// We can't handle Active structs, because that would mean (in the general case), that we would
118124
// need to allocate and initialize arbitrary user types. We have Duplicated/Dual input args for
119125
// that. FIXME: Give a nicer error and suggest to the user to have a `&mut Foo` input instead.
120-
#[autodiff(df14, Reverse, Active, Active)]
121-
fn f14(x: f32) -> Foo {
126+
#[autodiff(df15, Reverse, Active, Active)]
127+
fn f15(x: f32) -> Foo {
122128
unimplemented!()
123129
}
124130

@@ -127,15 +133,15 @@ type MyFloat = f32;
127133
// We would like to support type alias to f32/f64 in argument type in the future,
128134
// but that requires us to implement our checks at a later stage
129135
// like THIR which has type information available.
130-
#[autodiff(df15, Reverse, Active, Active)]
131-
fn f15(x: MyFloat) -> f32 {
136+
#[autodiff(df16, Reverse, Active, Active)]
137+
fn f16(x: MyFloat) -> f32 {
132138
//~^^ ERROR failed to resolve: use of undeclared type `MyFloat` [E0433]
133139
unimplemented!()
134140
}
135141

136142
// We would like to support type alias to f32/f64 in return type in the future
137-
#[autodiff(df16, Reverse, Active, Active)]
138-
fn f16(x: f32) -> MyFloat {
143+
#[autodiff(df17, Reverse, Active, Active)]
144+
fn f17(x: f32) -> MyFloat {
139145
unimplemented!()
140146
}
141147

@@ -145,34 +151,34 @@ struct F64Trans {
145151
}
146152

147153
// We would like to support `#[repr(transparent)]` f32/f64 wrapper in return type in the future
148-
#[autodiff(df17, Reverse, Active, Active)]
149-
fn f17(x: f64) -> F64Trans {
154+
#[autodiff(df18, Reverse, Active, Active)]
155+
fn f18(x: f64) -> F64Trans {
150156
unimplemented!()
151157
}
152158

153159
// We would like to support `#[repr(transparent)]` f32/f64 wrapper in argument type in the future
154-
#[autodiff(df18, Reverse, Active, Active)]
155-
fn f18(x: F64Trans) -> f64 {
160+
#[autodiff(df19, Reverse, Active, Active)]
161+
fn f19(x: F64Trans) -> f64 {
156162
//~^^ ERROR failed to resolve: use of undeclared type `F64Trans` [E0433]
157163
unimplemented!()
158164
}
159165

160166
// Invalid return activity
161-
#[autodiff(df19, Forward, Dual, Active)]
162-
fn f19(x: f32) -> f32 {
167+
#[autodiff(df20, Forward, Dual, Active)]
168+
fn f20(x: f32) -> f32 {
163169
//~^^ ERROR invalid return activity Active in Forward Mode
164170
unimplemented!()
165171
}
166172

167173
#[autodiff(df20, Reverse, Active, Dual)]
168-
fn f20(x: f32) -> f32 {
174+
fn f21(x: f32) -> f32 {
169175
//~^^ ERROR invalid return activity Dual in Reverse Mode
170176
unimplemented!()
171177
}
172178

173179
// Duplicated cannot be used as return activity
174180
#[autodiff(df21, Reverse, Active, Duplicated)]
175-
fn f21(x: f32) -> f32 {
181+
fn f22(x: f32) -> f32 {
176182
//~^^ ERROR invalid return activity Duplicated in Reverse Mode
177183
unimplemented!()
178184
}

tests/ui/autodiff/autodiff_illegal.stderr

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,24 @@ error: expected 1 activities, but found 2
1919
|
2020
LL | #[autodiff(df3, Reverse, Duplicated, Const)]
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22-
|
23-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
2422

2523
error: expected 1 activities, but found 0
2624
--> $DIR/autodiff_illegal.rs:27:1
2725
|
2826
LL | #[autodiff(df4, Reverse)]
2927
| ^^^^^^^^^^^^^^^^^^^^^^^^^
30-
|
31-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
3228

3329
error: Dual can not be used in Reverse Mode
3430
--> $DIR/autodiff_illegal.rs:34:1
3531
|
3632
LL | #[autodiff(df5, Reverse, Dual)]
3733
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
|
39-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
4034

4135
error: Duplicated can not be used in Forward Mode
4236
--> $DIR/autodiff_illegal.rs:41:1
4337
|
4438
LL | #[autodiff(df6, Forward, Duplicated)]
4539
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46-
|
47-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
4840

4941
error: Duplicated can not be used for this type
5042
--> $DIR/autodiff_illegal.rs:42:14
@@ -107,7 +99,6 @@ LL | #[autodiff(fn_exists, Reverse, Active)]
10799
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `fn_exists` redefined here
108100
|
109101
= note: `fn_exists` must be defined only once in the value namespace of this module
110-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
111102

112103
error: autodiff requires at least a name and mode
113104
--> $DIR/autodiff_illegal.rs:95:1
@@ -135,42 +126,52 @@ error: invalid return activity Active in Forward Mode
135126
|
136127
LL | #[autodiff(df19, Forward, Dual, Active)]
137128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138-
|
139-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
140129

141130
error: invalid return activity Dual in Reverse Mode
142131
--> $DIR/autodiff_illegal.rs:167:1
143132
|
144133
LL | #[autodiff(df20, Reverse, Active, Dual)]
145134
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146-
|
147-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
148135

149136
error: invalid return activity Duplicated in Reverse Mode
150137
--> $DIR/autodiff_illegal.rs:174:1
151138
|
152139
LL | #[autodiff(df21, Reverse, Active, Duplicated)]
153140
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141+
142+
error: expected 3 activities, but found 2
143+
--> $DIR/autodiff_illegal.rs:180:1
154144
|
155-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
145+
LL | #[autodiff(df, Forward, Dual, Dual)]
146+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
156147

157148
error[E0433]: failed to resolve: use of undeclared type `MyFloat`
158149
--> $DIR/autodiff_illegal.rs:130:1
159150
|
160151
LL | #[autodiff(df15, Reverse, Active, Active)]
161152
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `MyFloat`
162-
|
163-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
164153

165154
error[E0433]: failed to resolve: use of undeclared type `F64Trans`
166155
--> $DIR/autodiff_illegal.rs:154:1
167156
|
168157
LL | #[autodiff(df18, Reverse, Active, Active)]
169158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `F64Trans`
159+
160+
error[E0308]: mismatched types
161+
--> $DIR/autodiff_illegal.rs:181:25
162+
|
163+
LL | #[autodiff(df, Forward, Dual, Dual)]
164+
| -- implicitly returns `()` as its body has no tail or `return` expression
165+
LL | fn f(x: f32, y: f32) -> f32 {
166+
| ^^^ expected `f32`, found `()`
167+
|
168+
note: consider returning one of these bindings
169+
--> $DIR/autodiff_illegal.rs:181:6
170170
|
171-
= note: this error originates in the attribute macro `autodiff` (in Nightly builds, run with -Z macro-backtrace for more info)
171+
LL | fn f(x: f32, y: f32) -> f32 {
172+
| ^ ^
172173

173-
error: aborting due to 22 previous errors
174+
error: aborting due to 24 previous errors
174175

175-
Some errors have detailed explanations: E0428, E0433, E0658.
176-
For more information about an error, try `rustc --explain E0428`.
176+
Some errors have detailed explanations: E0308, E0428, E0433, E0658.
177+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)