From 4110636032f0fba089ed8779e7c2085e2e5b10ca Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Thu, 20 Mar 2025 12:03:26 +0100 Subject: [PATCH] Rust: preserve ordering in rust generated code This is a small devex improvement to the rust code generator. Usage of `sorted` in `rustgen.py` was causing the generated code to be completely reshuffled on renames, which made diffs hard to follow. As an example see [this generated file diff](https://github.com/github/codeql/pull/19059/files#diff-c938ba77a3398dd4c633ada5702a03477705c24740a2f7d1e40d4b270d8c3f86). This will make the order deterministically based on the order of definitions in the schema file. This means that renames will find the same place in the generated file, and the place in the generated file will generally be more predictable with respect to the schema. However, that does mean this change is heavily reshuffling the generated code. --- misc/codegen/generators/rustgen.py | 4 +- rust/extractor/src/generated/.generated.list | 2 +- rust/extractor/src/generated/top.rs | 3534 +++++++++--------- 3 files changed, 1771 insertions(+), 1769 deletions(-) diff --git a/misc/codegen/generators/rustgen.py b/misc/codegen/generators/rustgen.py index b47e5cc4bd9..38d2ae55d44 100644 --- a/misc/codegen/generators/rustgen.py +++ b/misc/codegen/generators/rustgen.py @@ -96,7 +96,9 @@ class Processor: name=name, fields=fields, detached_fields=detached_fields, - ancestors=sorted(set(a.name for a in _get_ancestors(cls, self._classmap))), + # remove duplicates but preserve ordering + # (`dict` preserves insertion order while `set` doesn't) + ancestors=[*{a.name: None for a in _get_ancestors(cls, self._classmap)}], entry_table=inflection.tableize(cls.name) if not cls.derived else None, ) diff --git a/rust/extractor/src/generated/.generated.list b/rust/extractor/src/generated/.generated.list index 7a3155e7afa..c80e596b7a8 100644 --- a/rust/extractor/src/generated/.generated.list +++ b/rust/extractor/src/generated/.generated.list @@ -1,2 +1,2 @@ mod.rs 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 -top.rs b290c599a2696c64c3391003de990c7036ac1990f43b507723d10519d18fcbee b290c599a2696c64c3391003de990c7036ac1990f43b507723d10519d18fcbee +top.rs 765a45234eacc510b2eeef9c44852766c3a818902108a2e921327804032bbc5a 765a45234eacc510b2eeef9c44852766c3a818902108a2e921327804032bbc5a diff --git a/rust/extractor/src/generated/top.rs b/rust/extractor/src/generated/top.rs index 7755ad50bc2..b797f7952fa 100644 --- a/rust/extractor/src/generated/top.rs +++ b/rust/extractor/src/generated/top.rs @@ -101,18 +101,18 @@ impl trap::TrapClass for AstNode { fn class_name() -> &'static str { "AstNode" } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Element + // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AstNode is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -158,18 +158,18 @@ impl trap::TrapClass for Crate { fn class_name() -> &'static str { "Crate" } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Crate is a subclass of Element + // SAFETY: this is safe because in the dbscheme Crate is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Crate is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Crate is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -195,18 +195,18 @@ impl trap::TrapClass for Missing { fn class_name() -> &'static str { "Missing" } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Missing is a subclass of Element + // SAFETY: this is safe because in the dbscheme Missing is a subclass of Unextracted unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Missing is a subclass of Unextracted + // SAFETY: this is safe because in the dbscheme Missing is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -232,18 +232,18 @@ impl trap::TrapClass for Unimplemented { fn class_name() -> &'static str { "Unimplemented" } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Unimplemented is a subclass of Element + // SAFETY: this is safe because in the dbscheme Unimplemented is a subclass of Unextracted unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Unimplemented is a subclass of Unextracted + // SAFETY: this is safe because in the dbscheme Unimplemented is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -282,18 +282,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Abi is a subclass of Element + // SAFETY: this is safe because in the dbscheme Abi is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Abi is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Abi is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -327,18 +327,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Addressable is a subclass of Element + // SAFETY: this is safe because in the dbscheme Addressable is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Addressable is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Addressable is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -377,18 +377,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ArgList is a subclass of Element + // SAFETY: this is safe because in the dbscheme ArgList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ArgList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme ArgList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -423,18 +423,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmDirSpec is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmDirSpec is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmDirSpec is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmDirSpec is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -459,18 +459,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOperand is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmOperand is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOperand is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmOperand is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -513,18 +513,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOperandExpr is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmOperandExpr is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOperandExpr is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmOperandExpr is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -563,18 +563,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOption is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmOption is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmOption is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmOption is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -599,18 +599,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmPiece is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmPiece is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmPiece is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmPiece is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -649,18 +649,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmRegSpec is a subclass of Element + // SAFETY: this is safe because in the dbscheme AsmRegSpec is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AsmRegSpec is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AsmRegSpec is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -685,18 +685,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AssocItem is a subclass of Element + // SAFETY: this is safe because in the dbscheme AssocItem is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AssocItem is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AssocItem is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -739,18 +739,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AssocItemList is a subclass of Element + // SAFETY: this is safe because in the dbscheme AssocItemList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme AssocItemList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme AssocItemList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -789,18 +789,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Attr is a subclass of Element + // SAFETY: this is safe because in the dbscheme Attr is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Attr is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Attr is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -825,18 +825,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Callable is a subclass of Element + // SAFETY: this is safe because in the dbscheme Callable is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Callable is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Callable is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -875,18 +875,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ClosureBinder is a subclass of Element + // SAFETY: this is safe because in the dbscheme ClosureBinder is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ClosureBinder is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme ClosureBinder is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -911,18 +911,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Expr is a subclass of Element + // SAFETY: this is safe because in the dbscheme Expr is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme Expr is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme Expr is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -947,18 +947,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ExternItem is a subclass of Element + // SAFETY: this is safe because in the dbscheme ExternItem is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ExternItem is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme ExternItem is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1001,18 +1001,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ExternItemList is a subclass of Element + // SAFETY: this is safe because in the dbscheme ExternItemList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ExternItemList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme ExternItemList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1037,18 +1037,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme FieldList is a subclass of Element + // SAFETY: this is safe because in the dbscheme FieldList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme FieldList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme FieldList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1091,18 +1091,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme FormatArgsArg is a subclass of Element + // SAFETY: this is safe because in the dbscheme FormatArgsArg is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme FormatArgsArg is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme FormatArgsArg is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1127,18 +1127,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericArg is a subclass of Element + // SAFETY: this is safe because in the dbscheme GenericArg is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericArg is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme GenericArg is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1177,18 +1177,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericArgList is a subclass of Element + // SAFETY: this is safe because in the dbscheme GenericArgList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericArgList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme GenericArgList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1213,18 +1213,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericParam is a subclass of Element + // SAFETY: this is safe because in the dbscheme GenericParam is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericParam is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme GenericParam is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1263,18 +1263,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericParamList is a subclass of Element + // SAFETY: this is safe because in the dbscheme GenericParamList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme GenericParamList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme GenericParamList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1317,18 +1317,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ItemList is a subclass of Element + // SAFETY: this is safe because in the dbscheme ItemList is a subclass of Locatable unsafe { Self::from_untyped(value.as_untyped()) } } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label) -> Self { - // SAFETY: this is safe because in the dbscheme ItemList is a subclass of Locatable + // SAFETY: this is safe because in the dbscheme ItemList is a subclass of Element unsafe { Self::from_untyped(value.as_untyped()) } @@ -1367,18 +1367,18 @@ impl From> for trap::Label { } } -impl From> for trap::Label { +impl From> for trap::Label { fn from(value: trap::Label