Skip to content

Commit baec6cc

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

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
lines changed

tests/ui/autodiff/autodiff_illegal.rs

Lines changed: 36 additions & 30 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,52 +68,52 @@ fn dummy() {
6268

6369
// Malformed, where args?
6470
#[autodiff]
65-
pub fn f7(x: f64) {
66-
//~^ ERROR autodiff requires at least a name and mode
71+
pub fn f8(x: f64) {
72+
//~^ 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) {
80-
//~^ ERROR autodiff requires at least a name and mode
85+
pub fn f10(x: f64) {
86+
//~^ ERROR autodiff must be applied to function
8187
unimplemented!()
8288
}
8389

8490
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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ error: invalid return activity Duplicated in Reverse Mode
139139
LL | #[autodiff(df21, Reverse, Active, Duplicated)]
140140
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141141

142+
error: expected 3 activities, but found 2
143+
--> $DIR/autodiff_illegal.rs:180:1
144+
|
145+
LL | #[autodiff(df, Forward, Dual, Dual)]
146+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147+
142148
error[E0433]: failed to resolve: use of undeclared type `MyFloat`
143149
--> $DIR/autodiff_illegal.rs:130:1
144150
|
@@ -151,6 +157,20 @@ error[E0433]: failed to resolve: use of undeclared type `F64Trans`
151157
LL | #[autodiff(df18, Reverse, Active, Active)]
152158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `F64Trans`
153159

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
170+
|
171+
LL | fn f(x: f32, y: f32) -> f32 {
172+
| ^ ^
173+
154174
error[E0599]: the function or associated item `default` exists for tuple `(DoesNotImplDefault, DoesNotImplDefault)`, but its trait bounds were not satisfied
155175
--> $DIR/autodiff_illegal.rs:181:1
156176
|
@@ -168,7 +188,7 @@ LL + #[derive(Default)]
168188
LL | struct DoesNotImplDefault;
169189
|
170190

171-
error: aborting due to 23 previous errors
191+
error: aborting due to 22 previous errors
172192

173-
Some errors have detailed explanations: E0428, E0433, E0599, E0658.
193+
Some errors have detailed explanations: E0428, E0433, E0658.
174194
For more information about an error, try `rustc --explain E0428`.

0 commit comments

Comments
 (0)