Rust: emit Const bodies in library mode

This commit is contained in:
Paolo Tranquilli
2025-06-02 16:52:18 +02:00
parent 1110fea2a2
commit f88949a34e
18 changed files with 16 additions and 88 deletions

View File

@@ -262,7 +262,7 @@ fn get_additional_fields(node: &AstNodeSrc) -> Vec<FieldInfo> {
"FormatArgsExpr" => vec![FieldInfo::list("args", "FormatArgsArg")],
"ArgList" => vec![FieldInfo::list("args", "Expr")],
"Fn" => vec![FieldInfo::body("body", "BlockExpr")],
"Const" => vec![FieldInfo::body("body", "Expr")],
"Const" => vec![FieldInfo::optional("body", "Expr")],
"Static" => vec![FieldInfo::body("body", "Expr")],
"Param" => vec![FieldInfo::body("pat", "Pat")],
"ClosureExpr" => vec![FieldInfo::optional("body", "Expr")],

View File

@@ -2816,11 +2816,6 @@ const_visibilities(
int visibility: @visibility ref
);
#keyset[id]
const_has_implementation(
int id: @const ref
);
enums(
unique int id: @enum
);

View File

@@ -0,0 +1,4 @@
description: Remove `function_has_implementation` table
compatibility: full
function_has_implementation.rel: delete

View File

@@ -1,5 +0,0 @@
description: Remove `function_has_implementation` and `const_has_implementation` tables
compatibility: full
function_has_implementation.rel: delete
const_has_implementation.rel: delete

View File

@@ -1,2 +1,2 @@
mod.rs 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7
top.rs 69c1fcaf0efea87feb898f32fdb7bcb842a22119b69ecedd61c2d946eb7e67de 69c1fcaf0efea87feb898f32fdb7bcb842a22119b69ecedd61c2d946eb7e67de
top.rs 53fb95b1d3978c70a1a4ea72984c956b77ae26f06eef469c6631d945c25e9081 53fb95b1d3978c70a1a4ea72984c956b77ae26f06eef469c6631d945c25e9081

View File

@@ -9084,12 +9084,6 @@ impl trap::TrapEntry for Const {
}
}
impl Const {
pub fn emit_has_implementation(id: trap::Label<Self>, out: &mut trap::Writer) {
out.add_tuple("const_has_implementation", vec![id.into()]);
}
}
impl trap::TrapClass for Const {
fn class_name() -> &'static str { "Const" }
}

View File

@@ -84,9 +84,6 @@ macro_rules! post_emit {
(PathSegment, $self:ident, $node:ident, $label:ident) => {
$self.extract_types_from_path_segment($node, $label.into());
};
(Const, $self:ident, $node:ident, $label:ident) => {
$self.emit_const_has_implementation($node, $label);
};
($($_:tt)*) => {};
}
@@ -775,14 +772,4 @@ impl<'a> Translator<'a> {
generated::Function::emit_has_implementation(label, &mut self.trap.writer);
}
}
pub(crate) fn emit_const_has_implementation(
&mut self,
node: &ast::Const,
label: Label<generated::Const>,
) {
if node.body().is_some() {
generated::Const::emit_has_implementation(label, &mut self.trap.writer);
}
}
}

View File

@@ -823,11 +823,7 @@ impl Translator<'_> {
return None;
}
let attrs = node.attrs().filter_map(|x| self.emit_attr(&x)).collect();
let body = if self.should_skip_bodies() {
None
} else {
node.body().and_then(|x| self.emit_expr(&x))
};
let body = node.body().and_then(|x| self.emit_expr(&x));
let is_const = node.const_token().is_some();
let is_default = node.default_token().is_some();
let name = node.name().and_then(|x| self.emit_name(&x));

View File

@@ -498,7 +498,7 @@ lib/codeql/rust/elements/internal/generated/CastExpr.qll ddc20054b0b339ad4d40298
lib/codeql/rust/elements/internal/generated/ClosureBinder.qll 94c0dcdd4cd87d115659d496c88a98354bc7d4ddc0fa27028003bf7688b99987 d59d713b426dbbdb775df9092d176eea031dac1f14e468810f2fc8591399cd19
lib/codeql/rust/elements/internal/generated/ClosureExpr.qll 34149bf82f107591e65738221e1407ec1dc9cc0dfb10ae7f761116fda45162de fd2fbc9a87fc0773c940db64013cf784d5e4137515cc1020e2076da329f5a952
lib/codeql/rust/elements/internal/generated/Comment.qll cd1ef861e3803618f9f78a4ac00516d50ecfecdca1c1d14304dc5327cbe07a3b 8b67345aeb15beb5895212228761ea3496297846c93fd2127b417406ae87c201
lib/codeql/rust/elements/internal/generated/Const.qll 56d954fe4f24a599e764ef926d6b42db8794c109b77851d1a5e81e0005d22610 30ac1d79bd4dbf84fe5d02b88ead0c1408e3b47254dc8f37703718ca81ebbefc
lib/codeql/rust/elements/internal/generated/Const.qll 03bd9bb84becc0716e12e8a788ab07098e568c58b43b63ed0d333b1c9e227ab7 3168e7b4cb551b9fde74967847576dada05f12a49a1b19c6900e0de32651bcd4
lib/codeql/rust/elements/internal/generated/ConstArg.qll e2451cac6ee464f5b64883d60d534996fcff061a520517ac792116238a11e185 1dd6d4b073b0970448a52bbe2468cd160dfe108971dbf9ae9305900bd22ef146
lib/codeql/rust/elements/internal/generated/ConstBlockPat.qll 7526d83ee9565d74776f42db58b1a2efff6fb324cfc7137f51f2206fee815d79 0ab3c22908ff790e7092e576a5df3837db33c32a7922a513a0f5e495729c1ac5
lib/codeql/rust/elements/internal/generated/ConstParam.qll 310342603959a4d521418caec45b585b97e3a5bf79368769c7150f52596a7266 a5dd92f0b24d7dbdaea2daedba3c8d5f700ec7d3ace81ca368600da2ad610082
@@ -578,7 +578,7 @@ lib/codeql/rust/elements/internal/generated/ParamList.qll c808c9d84dd7800573832b
lib/codeql/rust/elements/internal/generated/ParenExpr.qll bc0731505bfe88516205ec360582a4222d2681d11342c93e15258590ddee82f2 d4bd6e0c80cf1d63746c88d4bcb3a01d4c75732e5da09e3ebd9437ced227fb60
lib/codeql/rust/elements/internal/generated/ParenPat.qll 4f168ef5d5bb87a903251cc31b2e44a759b099ec69c90af31783fbb15778c940 0e34f94a45a13396fd57d94c245dc64d1adde2ab0e22b56946f7e94c04e297fc
lib/codeql/rust/elements/internal/generated/ParenTypeRepr.qll 40ab5c592e7699c621787793743e33988de71ff42ca27599f5ab3ddb70e3f7d8 12c0a6eed2202ee3e892f61da3b3ce77ac3190854cdf3097e8d2be98aa3cb91d
lib/codeql/rust/elements/internal/generated/ParentChild.qll ca2dcc42bf0c9d004dd54d2b131d807fa4e97abbc8bb4d9176506efb15032bf7 61cf70eb649f241e2fcd5e0ba34df63f3a14f07032811b9ae151721783a0fd20
lib/codeql/rust/elements/internal/generated/ParentChild.qll d7cf3e6870939004c5ff68f7cf0261511f0f82e106fddda9016d486cde0bb2b7 61cf70eb649f241e2fcd5e0ba34df63f3a14f07032811b9ae151721783a0fd20
lib/codeql/rust/elements/internal/generated/ParenthesizedArgList.qll c5fa328ea60d3a3333d7c7bb3480969c1873166c7ac8ebb9d0afad7a8099d1a8 2dbbb6200d96f7db7dea4a55bdeab8d67b14d39a43e0bd54ada019f7e466f163
lib/codeql/rust/elements/internal/generated/Pat.qll 3605ac062be2f294ee73336e9669027b8b655f4ad55660e1eab35266275154ee 7f9400db2884d336dd1d21df2a8093759c2a110be9bf6482ce8e80ae0fd74ed4
lib/codeql/rust/elements/internal/generated/Path.qll 9b12afb46fc5a9ad3a811b05472621bbecccb900c47504feb7f29d96b28421ca bcacbffc36fb3e0c9b26523b5963af0ffa9fd6b19f00a2a31bdb2316071546bd
@@ -593,7 +593,7 @@ lib/codeql/rust/elements/internal/generated/PtrTypeRepr.qll 51d1e9e683fc79dddbff
lib/codeql/rust/elements/internal/generated/PureSynthConstructors.qll e5b8e69519012bbaae29dcb82d53f7f7ecce368c0358ec27ef6180b228a0057f e5b8e69519012bbaae29dcb82d53f7f7ecce368c0358ec27ef6180b228a0057f
lib/codeql/rust/elements/internal/generated/RangeExpr.qll 23cca03bf43535f33b22a38894f70d669787be4e4f5b8fe5c8f7b964d30e9027 18624cef6c6b679eeace2a98737e472432e0ead354cca02192b4d45330f047c9
lib/codeql/rust/elements/internal/generated/RangePat.qll 80826a6a6868a803aa2372e31c52a03e1811a3f1f2abdb469f91ca0bfdd9ecb6 34ee1e208c1690cba505dff2c588837c0cd91e185e2a87d1fe673191962276a9
lib/codeql/rust/elements/internal/generated/Raw.qll cac788ae82503c953f6260665bf6be08c3dea1a3136186cac3e5242aa64abe13 10a41c862eb0bf8968665a3ce545f1effd38b5f97fec8a35b544592371da5771
lib/codeql/rust/elements/internal/generated/Raw.qll 9c612b746397d931a453ca89a52cfc8f9a7150d642a0467c4694432f21e2cc14 2efd3e21cb45cc4dad0d1a78a3e857d1163d1214eb83e9951b76431d4fe2a32a
lib/codeql/rust/elements/internal/generated/RefExpr.qll 7d995884e3dc1c25fc719f5d7253179344d63650e217e9ff6530285fe7a57f64 f2c3c12551deea4964b66553fb9b6423ee16fec53bd63db4796191aa60dc6c66
lib/codeql/rust/elements/internal/generated/RefPat.qll 456ede39837463ee22a630ec7ab6c8630d3664a8ea206fcc6e4f199e92fa564c 5622062765f32930465ba6b170e986706f159f6070f48adee3c20e24e8df4e05
lib/codeql/rust/elements/internal/generated/RefTypeRepr.qll 3d8c0bd296d33b91a81633f697a43269a6538df06d277262d3990d3f6880ef57 13680f39e89bcd8299c218aba396f3deec804597e6f7cb7d4a7e7c748b6faa77
@@ -735,7 +735,7 @@ test/extractor-tests/generated/ClosureExpr/ClosureExpr_getClosureBinder.ql cbfcf
test/extractor-tests/generated/ClosureExpr/ClosureExpr_getParamList.ql 68ce501516094512dd5bfed42a785474583a91312f704087cba801b02ba7b834 eacbf89d63159e7decfd84c2a1dc5c067dfce56a8157fbb52bc133e9702d266d
test/extractor-tests/generated/ClosureExpr/ClosureExpr_getRetType.ql c95bc7306b2d77aa05a6501b6321e6f1e7a48b7ad422ba082635ab20014288ae fe72d44c9819b42fff49b9092a9fb2bfafde6d3b9e4967547fb5298822f30bc3
test/extractor-tests/generated/Comment/Comment.ql 5428b8417a737f88f0d55d87de45c4693d81f03686f03da11dc5369e163d977b 8948c1860cde198d49cff7c74741f554a9e89f8af97bb94de80f3c62e1e29244
test/extractor-tests/generated/Const/Const.ql 7f9c9ce5b04391ff8f0d419a62972292b1d9811a978cceb129ef5a0f68a02fab c6c1dfa688248310cadd2d6d71d1a70a2b48f5b29ea2078f4dd51db59a0021c0
test/extractor-tests/generated/Const/Const.ql 6794d0056060a82258d1e832ad265e2eb276206f0224a3f0eb9221e225370066 0a6134fb5a849ce9bd1a28de783460301cafca5773bd7caa4fb1f774f81b476a
test/extractor-tests/generated/Const/Const_getAttr.ql bd6296dab00065db39663db8d09fe62146838875206ff9d8595d06d6439f5043 34cb55ca6d1f44e27d82a8b624f16f9408bae2485c85da94cc76327eed168577
test/extractor-tests/generated/Const/Const_getAttributeMacroExpansion.ql 82e86399d5cd72621dc8d9cd9f310d3dc7f2ecf208149dab0d202047ccbbd2f8 33df8c5b5044f49ec244e183c61c3b81fabd987f590ba6da4e18e08231343dc8
test/extractor-tests/generated/Const/Const_getBody.ql f50f79b7f42bb1043b79ec96f999fa4740c8014e6969a25812d5d023d7a5a5d8 90e5060ba9757f1021429ed4ec4913bc78747f3fc415456ef7e7fc284b8a0026

View File

@@ -106,15 +106,5 @@ module Generated {
* Holds if `getVisibility()` exists.
*/
final predicate hasVisibility() { exists(this.getVisibility()) }
/**
* Holds if this constant has an implementation.
*
* This is the same as `hasBody` for source code, but for library code (for which we always skip
* the body), this will hold when the body was present in the original code.
*/
predicate hasImplementation() {
Synth::convertConstToRaw(this).(Raw::Const).hasImplementation()
}
}
}

View File

@@ -3350,14 +3350,6 @@ module Raw {
* Gets the visibility of this const, if it exists.
*/
Visibility getVisibility() { const_visibilities(this, result) }
/**
* Holds if this constant has an implementation.
*
* This is the same as `hasBody` for source code, but for library code (for which we always skip
* the body), this will hold when the body was present in the original code.
*/
predicate hasImplementation() { const_has_implementation(this) }
}
/**

View File

@@ -2816,11 +2816,6 @@ const_visibilities(
int visibility: @visibility ref
);
#keyset[id]
const_has_implementation(
int id: @const ref
);
enums(
unique int id: @enum
);

View File

@@ -2816,11 +2816,6 @@ const_visibilities(
int visibility: @visibility ref
);
#keyset[id]
const_has_implementation(
int id: @const ref
);
enums(
unique int id: @enum
);

View File

@@ -1,5 +1,4 @@
description: Add `function_has_implementation` and `const_has_implementation` tables
description: Add `function_has_implementation` table
compatibility: backwards
function_has_implementation.rel: run upgrade.ql new_function_has_implementation
const_has_implementation.rel: run upgrade.ql new_const_has_implementation
function_has_implementation.rel: reorder function_bodies.rel (@function id, @block_expr body) id

View File

@@ -1,7 +0,0 @@
class Element extends @element {
string toString() { none() }
}
query predicate new_function_has_implementation(Element e) { function_bodies(e, _) }
query predicate new_const_has_implementation(Element e) { const_bodies(e, _) }

View File

@@ -5,8 +5,7 @@ import TestUtils
from
Const x, string hasExtendedCanonicalPath, string hasCrateOrigin,
string hasAttributeMacroExpansion, int getNumberOfAttrs, string hasBody, string isConst,
string isDefault, string hasName, string hasTypeRepr, string hasVisibility,
string hasImplementation
string isDefault, string hasName, string hasTypeRepr, string hasVisibility
where
toBeTested(x) and
not x.isUnknown() and
@@ -27,10 +26,8 @@ where
(if x.isDefault() then isDefault = "yes" else isDefault = "no") and
(if x.hasName() then hasName = "yes" else hasName = "no") and
(if x.hasTypeRepr() then hasTypeRepr = "yes" else hasTypeRepr = "no") and
(if x.hasVisibility() then hasVisibility = "yes" else hasVisibility = "no") and
if x.hasImplementation() then hasImplementation = "yes" else hasImplementation = "no"
if x.hasVisibility() then hasVisibility = "yes" else hasVisibility = "no"
select x, "hasExtendedCanonicalPath:", hasExtendedCanonicalPath, "hasCrateOrigin:", hasCrateOrigin,
"hasAttributeMacroExpansion:", hasAttributeMacroExpansion, "getNumberOfAttrs:", getNumberOfAttrs,
"hasBody:", hasBody, "isConst:", isConst, "isDefault:", isDefault, "hasName:", hasName,
"hasTypeRepr:", hasTypeRepr, "hasVisibility:", hasVisibility, "hasImplementation:",
hasImplementation
"hasTypeRepr:", hasTypeRepr, "hasVisibility:", hasVisibility

View File

@@ -951,10 +951,6 @@ class _:
todo!()
```
"""
has_implementation: predicate | doc("this constant has an implementation") | desc("""
This is the same as `hasBody` for source code, but for library code (for which we always skip
the body), this will hold when the body was present in the original code.
""") | rust.detach
@annotate(ConstArg)