Sitelet https://github.com/starkware-libs/cairo/commit/f4957f3a81398e298431550dea6932f0ed3d13e8
Skip to content

Commit f4957f3

Browse files
committed
feat(plugins): deprecate #[panic_with]
Emit a warning when `#[panic_with]` is used, unless the item or an enclosing module grants `#[feature("deprecated-panic-with")]`. Prefer explicit calculations, which are now supported in const context. The wrapper is still generated — this only warns. Relies on `MacroPluginMetadata::allowed_features` being populated at the defs layer.
1 parent ccf3121 commit f4957f3

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎crates/cairo-lang-plugins/src/plugins/panicable.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ use salsa::Database;
1818
pub struct PanicablePlugin;
1919

2020
const PANIC_WITH_ATTR: &str = "panic_with";
21+
/// The feature gating the deprecated `#[panic_with]` macro. Stored with quotes, as
22+
/// `allowed_features` holds the raw string-literal text.
23+
const DEPRECATED_PANIC_WITH_FEATURE: &str = r#""deprecated-panic-with""#;
2124

2225
impl MacroPlugin for PanicablePlugin {
2326
fn generate_code<'db>(
2427
&self,
2528
db: &'db dyn Database,
2629
item_ast: ast::ModuleItem<'db>,
27-
_metadata: &MacroPluginMetadata<'_>,
30+
metadata: &MacroPluginMetadata<'_>,
2831
) -> PluginResult<'db> {
2932
let (declaration, attributes, visibility) = match item_ast {
3033
ast::ModuleItem::ExternFunction(extern_func_ast) => (
@@ -40,7 +43,7 @@ impl MacroPlugin for PanicablePlugin {
4043
_ => return PluginResult::default(),
4144
};
4245

43-
generate_panicable_code(db, declaration, attributes, visibility)
46+
generate_panicable_code(db, declaration, attributes, visibility, metadata)
4447
}
4548

4649
fn declared_attributes<'db>(&self, db: &'db dyn Database) -> Vec<SmolStrId<'db>> {
@@ -54,13 +57,25 @@ fn generate_panicable_code<'db>(
5457
declaration: ast::FunctionDeclaration<'db>,
5558
attributes: ast::AttributeList<'db>,
5659
visibility: ast::Visibility<'db>,
60+
metadata: &MacroPluginMetadata<'_>,
5761
) -> PluginResult<'db> {
5862
let mut attrs = attributes.query_attr(db, PANIC_WITH_ATTR);
5963
let Some(attr) = attrs.next() else {
6064
// No `#[panic_with]` attribute found.
6165
return PluginResult::default();
6266
};
6367
let mut diagnostics = vec![];
68+
let deprecation_feature = SmolStrId::from(db, DEPRECATED_PANIC_WITH_FEATURE);
69+
if !metadata.allowed_features.contains(&deprecation_feature) {
70+
diagnostics.push(PluginDiagnostic::warning(
71+
attr.stable_ptr(db),
72+
format!(
73+
"Usage of deprecated macro `{PANIC_WITH_ATTR}` with no \
74+
`#[feature({DEPRECATED_PANIC_WITH_FEATURE})]` attribute. Use simple calculations \
75+
instead, as these are supported in const context.",
76+
),
77+
));
78+
}
6479
if let Some(extra_attr) = attrs.next() {
6580
diagnostics.push(PluginDiagnostic::error(
6681
extra_attr.stable_ptr(db),

‎crates/cairo-lang-plugins/src/test_data/panicable‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,50 @@
44
test_expand_plugin(expect_diagnostics: false)
55

66
//! > cairo_code
7+
#[feature("deprecated-panic-with")]
78
#[panic_with('1', foo_improved)]
89
extern fn foo(a: felt252, ref b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
910

11+
#[feature("deprecated-panic-with")]
1012
#[panic_with('2', bar_changed)]
1113
extern fn bar() -> Result<felt252, Err> nopanic;
1214

15+
#[feature("deprecated-panic-with")]
1316
#[panic_with('3', non_extern_stuff)]
1417
fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic {
1518
(4, 56)
1619
}
1720

21+
#[feature("deprecated-panic-with")]
1822
#[panic_with('4', generic_panic)]
1923
extern fn generic<T>(t: T, v: felt252) -> Result<T, Err> nopanic;
2024

25+
#[feature("deprecated-panic-with")]
2126
#[panic_with('5', wrapped_possibly_panic)]
2227
fn possibly_panic() -> Result<felt252, Err> {
2328
Result::<felt252, Err>::Ok(1)
2429
}
2530

2631
//! > expanded_cairo_code
32+
#[feature("deprecated-panic-with")]
2733
#[panic_with('1', foo_improved)]
2834
extern fn foo(a: felt252, ref b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
2935

36+
#[feature("deprecated-panic-with")]
3037
#[panic_with('2', bar_changed)]
3138
extern fn bar() -> Result<felt252, Err> nopanic;
3239

40+
#[feature("deprecated-panic-with")]
3341
#[panic_with('3', non_extern_stuff)]
3442
fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic {
3543
(4, 56)
3644
}
3745

46+
#[feature("deprecated-panic-with")]
3847
#[panic_with('4', generic_panic)]
3948
extern fn generic<T>(t: T, v: felt252) -> Result<T, Err> nopanic;
4049

50+
#[feature("deprecated-panic-with")]
4151
#[panic_with('5', wrapped_possibly_panic)]
4252
fn possibly_panic() -> Result<felt252, Err> {
4353
Result::<felt252, Err>::Ok(1)
@@ -111,24 +121,48 @@ extern fn bad_ret_type(_a: some_type) -> felt252 nopanic;
111121
extern fn bar() -> Result<felt252, Err> nopanic;
112122

113123
//! > expected_diagnostics
124+
error: Usage of deprecated macro `panic_with` with no `#[feature("deprecated-panic-with")]` attribute. Use simple calculations instead, as these are supported in const context.
125+
--> test_src/lib.cairo:1:1
126+
#[panic_with(123, foo_bad_err_code)]
127+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
129+
114130
error: Failed to extract panic data attribute
115131
--> test_src/lib.cairo:1:1
116132
#[panic_with(123, foo_bad_err_code)]
117133
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118134

119135

136+
error: Usage of deprecated macro `panic_with` with no `#[feature("deprecated-panic-with")]` attribute. Use simple calculations instead, as these are supported in const context.
137+
--> test_src/lib.cairo:4:1
138+
#[panic_with(missing_args)]
139+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
140+
141+
120142
error: Failed to extract panic data attribute
121143
--> test_src/lib.cairo:4:1
122144
#[panic_with(missing_args)]
123145
^^^^^^^^^^^^^^^^^^^^^^^^^^^
124146

125147

148+
error: Usage of deprecated macro `panic_with` with no `#[feature("deprecated-panic-with")]` attribute. Use simple calculations instead, as these are supported in const context.
149+
--> test_src/lib.cairo:7:1
150+
#[panic_with(missing_args)]
151+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
152+
153+
126154
error: Currently only wrapping functions returning an Option<T> or Result<T, E>
127155
--> test_src/lib.cairo:8:39
128156
extern fn bad_ret_type(_a: some_type) -> felt252 nopanic;
129157
^^^^^^^^^^
130158

131159

160+
error: Usage of deprecated macro `panic_with` with no `#[feature("deprecated-panic-with")]` attribute. Use simple calculations instead, as these are supported in const context.
161+
--> test_src/lib.cairo:10:1
162+
#[panic_with('2', bar_changed)]
163+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164+
165+
132166
error: `#[panic_with]` cannot be applied multiple times to the same item.
133167
--> test_src/lib.cairo:11:1
134168
#[panic_with('3', bar_changed)]

0 commit comments

Comments
 (0)