Skip to content

Commit 1ad7b4b

Browse files
committed
Preserve attributes on interned/tracked structs
1 parent 8d3455f commit 1ad7b4b

11 files changed

+97
-4
lines changed

components/salsa-macro-rules/src/setup_interned_struct.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ macro_rules! setup_interned_struct {
5454
// Indexed types for each field (T0, T1, ...)
5555
field_indexed_tys: [$($indexed_ty:ident),*],
5656

57+
// Attrs for each field.
58+
field_attrs: [$([$(#[$field_attr:meta]),*]),*],
59+
5760
// Number of fields
5861
num_fields: $N:literal,
5962

@@ -211,6 +214,7 @@ macro_rules! setup_interned_struct {
211214
}
212215

213216
$(
217+
$(#[$field_attr])*
214218
$field_getter_vis fn $field_getter_id<$Db>(self, db: &'db $Db) -> $zalsa::return_mode_ty!($field_option, 'db, $field_ty)
215219
where
216220
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`

components/salsa-macro-rules/src/setup_tracked_struct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ macro_rules! setup_tracked_struct {
7272
// (see e.g. @return_mode below).
7373
untracked_options: [$($untracked_option:tt),*],
7474

75+
// Attrs for each field.
76+
tracked_field_attrs: [$([$(#[$tracked_field_attr:meta]),*]),*],
77+
untracked_field_attrs: [$([$(#[$untracked_field_attr:meta]),*]),*],
78+
7579
// Number of tracked fields.
7680
num_tracked_fields: $N:literal,
7781

@@ -256,6 +260,7 @@ macro_rules! setup_tracked_struct {
256260
}
257261

258262
$(
263+
$(#[$tracked_field_attr])*
259264
$tracked_getter_vis fn $tracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::return_mode_ty!($tracked_option, $db_lt, $tracked_ty)
260265
where
261266
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`
@@ -272,6 +277,7 @@ macro_rules! setup_tracked_struct {
272277
)*
273278

274279
$(
280+
$(#[$untracked_field_attr])*
275281
$untracked_getter_vis fn $untracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::return_mode_ty!($untracked_option, $db_lt, $untracked_ty)
276282
where
277283
// FIXME(rust-lang/rust#65991): The `db` argument *should* have the type `dyn Database`

components/salsa-macros/src/interned.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl Macro {
101101
let field_options = salsa_struct.field_options();
102102
let field_tys = salsa_struct.field_tys();
103103
let field_indexed_tys = salsa_struct.field_indexed_tys();
104+
let field_unused_attrs = salsa_struct.field_attrs();
104105
let generate_debug_impl = salsa_struct.generate_debug_impl();
105106
let has_lifetime = salsa_struct.generate_lifetime();
106107
let id = salsa_struct.id();
@@ -147,6 +148,7 @@ impl Macro {
147148
field_tys: [#(#field_tys),*],
148149
field_indices: [#(#field_indices),*],
149150
field_indexed_tys: [#(#field_indexed_tys),*],
151+
field_attrs: [#([#(#field_unused_attrs),*]),*],
150152
num_fields: #num_fields,
151153
generate_debug_impl: #generate_debug_impl,
152154
unused_names: [

components/salsa-macros/src/salsa_struct.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub(crate) struct SalsaField<'s> {
6363
pub(crate) has_no_eq_attr: bool,
6464
get_name: syn::Ident,
6565
set_name: syn::Ident,
66+
unknown_attrs: Vec<&'s syn::Attribute>,
6667
}
6768

6869
const BANNED_FIELD_NAMES: &[&str] = &["from", "new"];
@@ -316,6 +317,22 @@ where
316317
.collect()
317318
}
318319

320+
pub(crate) fn field_attrs(&self) -> Vec<&[&syn::Attribute]> {
321+
self.fields.iter().map(|f| &*f.unknown_attrs).collect()
322+
}
323+
324+
pub(crate) fn tracked_field_attrs(&self) -> Vec<&[&syn::Attribute]> {
325+
self.tracked_fields_iter()
326+
.map(|f| &*f.1.unknown_attrs)
327+
.collect()
328+
}
329+
330+
pub(crate) fn untracked_field_attrs(&self) -> Vec<&[&syn::Attribute]> {
331+
self.untracked_fields_iter()
332+
.map(|f| &*f.1.unknown_attrs)
333+
.collect()
334+
}
335+
319336
pub(crate) fn field_options(&self) -> Vec<TokenStream> {
320337
self.fields.iter().map(SalsaField::options).collect()
321338
}
@@ -377,15 +394,22 @@ impl<'s> SalsaField<'s> {
377394
has_no_eq_attr: false,
378395
get_name,
379396
set_name,
397+
unknown_attrs: Default::default(),
380398
};
381399

382400
// Scan the attributes and look for the salsa attributes:
383401
for attr in &field.attrs {
402+
let mut handled = false;
384403
for (fa, func) in FIELD_OPTION_ATTRIBUTES {
385404
if attr.path().is_ident(fa) {
386405
func(attr, &mut result);
406+
handled = true;
407+
break;
387408
}
388409
}
410+
if !handled {
411+
result.unknown_attrs.push(attr);
412+
}
389413
}
390414

391415
// Validate return mode

components/salsa-macros/src/tracked_struct.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ impl Macro {
111111
let tracked_tys = salsa_struct.tracked_tys();
112112
let untracked_tys = salsa_struct.untracked_tys();
113113

114+
let tracked_field_unused_attrs = salsa_struct.tracked_field_attrs();
115+
let untracked_field_unused_attrs = salsa_struct.untracked_field_attrs();
116+
114117
let num_tracked_fields = salsa_struct.num_tracked_fields();
115118
let generate_debug_impl = salsa_struct.generate_debug_impl();
116119

@@ -151,6 +154,9 @@ impl Macro {
151154
tracked_options: [#(#tracked_options),*],
152155
untracked_options: [#(#untracked_options),*],
153156

157+
tracked_field_attrs: [#([#(#tracked_field_unused_attrs),*]),*],
158+
untracked_field_attrs: [#([#(#untracked_field_unused_attrs),*]),*],
159+
154160
num_tracked_fields: #num_tracked_fields,
155161
generate_debug_impl: #generate_debug_impl,
156162
unused_names: [

src/table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl Page {
377377
}
378378

379379
#[inline]
380-
fn assert_type<T: Slot>(&self) -> PageView<T> {
380+
fn assert_type<T: Slot>(&self) -> PageView<'_, T> {
381381
assert_eq!(
382382
self.slot_type_id,
383383
TypeId::of::<T>(),
@@ -388,7 +388,7 @@ impl Page {
388388
PageView(self, PhantomData)
389389
}
390390

391-
fn cast_type<T: Slot>(&self) -> Option<PageView<T>> {
391+
fn cast_type<T: Slot>(&self) -> Option<PageView<'_, T>> {
392392
if self.slot_type_id == TypeId::of::<T>() {
393393
Some(PageView(self, PhantomData))
394394
} else {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[salsa::interned]
2+
struct UnknownAttributeInterned {
3+
/// Test doc comment
4+
field: bool,
5+
#[unknown_attr]
6+
field2: bool,
7+
#[salsa::tracked]
8+
wrong_tracked: bool,
9+
}
10+
11+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: only a single lifetime parameter is accepted
2+
--> tests/compile-fail/interned_struct_unknown_attribute.rs:1:1
3+
|
4+
1 | #[salsa::interned]
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the attribute macro `salsa::interned` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: cannot find attribute `unknown_attr` in this scope
10+
--> tests/compile-fail/interned_struct_unknown_attribute.rs:5:7
11+
|
12+
5 | #[unknown_attr]
13+
| ^^^^^^^^^^^^
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[salsa::tracked]
2+
struct UnknownAttributeTrackedStruct<'db> {
3+
#[tracked]
4+
tracked: bool,
5+
#[unknown_attr]
6+
field: bool,
7+
#[salsa::tracked]
8+
wrong_tracked: bool,
9+
/// TestDocComment
10+
/// TestDocComment
11+
field_with_doc: bool
12+
}
13+
14+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: only a single lifetime parameter is accepted
2+
--> tests/compile-fail/tracked_struct_unknown_attribute.rs:1:1
3+
|
4+
1 | #[salsa::tracked]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the attribute macro `salsa::tracked` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: cannot find attribute `unknown_attr` in this scope
10+
--> tests/compile-fail/tracked_struct_unknown_attribute.rs:5:7
11+
|
12+
5 | #[unknown_attr]
13+
| ^^^^^^^^^^^^

tests/cycle_tracked_own_input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ enum Type<'db> {
4242
}
4343

4444
impl Type<'_> {
45-
fn class(&self) -> Option<Class> {
45+
fn class(&self) -> Option<Class<'_>> {
4646
match self {
4747
Type::Class(class) => Some(*class),
4848
Type::Unknown => None,
@@ -79,7 +79,7 @@ fn infer_type_param<'db>(db: &'db dyn salsa::Database, node: TypeParamNode) -> T
7979
}
8080
}
8181

82-
fn infer_class_initial(_db: &dyn Database, _node: ClassNode) -> Type {
82+
fn infer_class_initial(_db: &'_ dyn Database, _node: ClassNode) -> Type<'_> {
8383
Type::Unknown
8484
}
8585

0 commit comments

Comments
 (0)