2024-10-11 20:38:43 +02:00
//@ needs-enzyme
#![ feature(autodiff) ]
//@ pretty-mode:expanded
//@ pretty-compare-only
//@ pp-exact:autodiff_illegal.pp
// Test that invalid ad macros give nice errors and don't ICE.
2025-05-10 21:50:06 +00:00
use std ::autodiff ::{ autodiff_forward , autodiff_reverse } ;
2024-10-11 20:38:43 +02:00
// We can't use Duplicated on scalars
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df1, Duplicated) ]
2024-10-11 20:38:43 +02:00
pub fn f1 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^ ERROR Duplicated can not be used for this type
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// Too many activities
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df3, Duplicated, Const) ]
2024-10-11 20:38:43 +02:00
pub fn f3 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR expected 1 activities, but found 2
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// To few activities
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df4) ]
2024-10-11 20:38:43 +02:00
pub fn f4 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR expected 1 activities, but found 0
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// We can't use Dual in Reverse mode
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df5, Dual) ]
2024-10-11 20:38:43 +02:00
pub fn f5 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR Dual can not be used in Reverse Mode
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// We can't use Duplicated in Forward mode
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df6, Duplicated) ]
2024-10-11 20:38:43 +02:00
pub fn f6 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR Duplicated can not be used in Forward Mode
//~^^ ERROR Duplicated can not be used for this type
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
fn dummy ( ) {
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df7, Dual) ]
2024-10-11 20:38:43 +02:00
let mut x = 5 ;
//~^ ERROR autodiff must be applied to function
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df7, Dual) ]
2024-10-11 20:38:43 +02:00
x = x + 3 ;
//~^^ ERROR attributes on expressions are experimental [E0658]
//~^^ ERROR autodiff must be applied to function
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df7, Dual) ]
2024-10-11 20:38:43 +02:00
let add_one_v2 = | x : u32 | -> u32 { x + 1 } ;
//~^ ERROR autodiff must be applied to function
}
// Malformed, where args?
2025-05-10 21:50:06 +00:00
#[ autodiff_forward ]
2024-10-11 20:38:43 +02:00
pub fn f7 ( x : f64 ) {
2025-04-08 21:54:34 -04:00
//~^ ERROR autodiff requires at least a name and mode
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// Malformed, where args?
2025-05-10 21:50:06 +00:00
#[ autodiff_forward() ]
2024-10-11 20:38:43 +02:00
pub fn f8 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^ ERROR autodiff requires at least a name and mode
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// Invalid attribute syntax
2025-05-10 21:50:06 +00:00
#[ autodiff_forward = " " ]
2024-10-11 20:38:43 +02:00
pub fn f9 ( x : f64 ) {
2025-04-08 21:54:34 -04:00
//~^ ERROR autodiff requires at least a name and mode
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
fn fn_exists ( ) { }
// We colide with an already existing function
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(fn_exists, Active) ]
2024-10-11 20:38:43 +02:00
pub fn f10 ( x : f64 ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR the name `fn_exists` is defined multiple times [E0428]
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// Invalid, please pick one Mode
// or use two autodiff macros.
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df13, Reverse) ]
2024-10-11 20:38:43 +02:00
pub fn f13 ( ) {
2025-03-07 17:37:50 +01:00
//~^^ ERROR did not recognize Activity: `Reverse`
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
struct Foo { }
// We can't handle Active structs, because that would mean (in the general case), that we would
// need to allocate and initialize arbitrary user types. We have Duplicated/Dual input args for
// that. FIXME: Give a nicer error and suggest to the user to have a `&mut Foo` input instead.
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df14, Active, Active) ]
2024-10-11 20:38:43 +02:00
fn f14 ( x : f32 ) -> Foo {
unimplemented! ( )
}
type MyFloat = f32 ;
// We would like to support type alias to f32/f64 in argument type in the future,
// but that requires us to implement our checks at a later stage
// like THIR which has type information available.
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df15, Active, Active) ]
2024-10-11 20:38:43 +02:00
fn f15 ( x : MyFloat ) -> f32 {
2025-03-07 17:37:50 +01:00
//~^^ ERROR failed to resolve: use of undeclared type `MyFloat` [E0433]
2024-10-11 20:38:43 +02:00
unimplemented! ( )
}
// We would like to support type alias to f32/f64 in return type in the future
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df16, Active, Active) ]
2024-10-11 20:38:43 +02:00
fn f16 ( x : f32 ) -> MyFloat {
unimplemented! ( )
}
#[ repr(transparent) ]
2025-03-07 17:37:50 +01:00
struct F64Trans {
inner : f64 ,
}
2024-10-11 20:38:43 +02:00
// We would like to support `#[repr(transparent)]` f32/f64 wrapper in return type in the future
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df17, Active, Active) ]
2024-10-11 20:38:43 +02:00
fn f17 ( x : f64 ) -> F64Trans {
unimplemented! ( )
}
// We would like to support `#[repr(transparent)]` f32/f64 wrapper in argument type in the future
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df18, Active, Active) ]
2024-10-11 20:38:43 +02:00
fn f18 ( x : F64Trans ) -> f64 {
//~^^ ERROR failed to resolve: use of undeclared type `F64Trans` [E0433]
unimplemented! ( )
}
2025-03-07 17:37:50 +01:00
// Invalid return activity
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df19, Dual, Active) ]
2025-03-07 17:37:50 +01:00
fn f19 ( x : f32 ) -> f32 {
//~^^ ERROR invalid return activity Active in Forward Mode
unimplemented! ( )
}
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df20, Active, Dual) ]
2025-03-07 17:37:50 +01:00
fn f20 ( x : f32 ) -> f32 {
//~^^ ERROR invalid return activity Dual in Reverse Mode
unimplemented! ( )
}
// Duplicated cannot be used as return activity
2025-05-10 21:50:06 +00:00
#[ autodiff_reverse(df21, Active, Duplicated) ]
2025-03-07 17:37:50 +01:00
fn f21 ( x : f32 ) -> f32 {
//~^^ ERROR invalid return activity Duplicated in Reverse Mode
unimplemented! ( )
}
2024-10-11 20:38:43 +02:00
2025-04-04 14:24:46 -04:00
struct DoesNotImplDefault ;
2025-05-10 21:50:06 +00:00
#[ autodiff_forward(df22, Dual) ]
2025-04-04 14:24:46 -04:00
pub fn f22 ( ) -> DoesNotImplDefault {
//~^^ ERROR the function or associated item `default` exists for tuple `(DoesNotImplDefault, DoesNotImplDefault)`, but its trait bounds were not satisfied
unimplemented! ( )
}
2024-10-11 20:38:43 +02:00
fn main ( ) { }