Rust: store in the DB if a library function or const had a body

When skipping bodies in library code, we lose the information whether a
body was originally present. This can be important, for example when
determining whether a trait method has a default implementation.

With this change that information can be recovered via the
`hasImplementation` predicate.
This commit is contained in:
Paolo Tranquilli
2025-06-02 16:15:37 +02:00
parent 77c40da51a
commit 667eed9b9b
11 changed files with 109 additions and 13 deletions

View File

@@ -1,2 +1,2 @@
mod.rs 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7 4bcb9def847469aae9d8649461546b7c21ec97cf6e63d3cf394e339915ce65d7
top.rs 7f8a694078bc0cde1ce420544d0cf5b83bb297dd29ee4f9d7bcbb1572f0e815a 7f8a694078bc0cde1ce420544d0cf5b83bb297dd29ee4f9d7bcbb1572f0e815a
top.rs 69c1fcaf0efea87feb898f32fdb7bcb842a22119b69ecedd61c2d946eb7e67de 69c1fcaf0efea87feb898f32fdb7bcb842a22119b69ecedd61c2d946eb7e67de

View File

@@ -9084,6 +9084,12 @@ 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" }
}
@@ -9497,6 +9503,12 @@ impl trap::TrapEntry for Function {
}
}
impl Function {
pub fn emit_has_implementation(id: trap::Label<Self>, out: &mut trap::Writer) {
out.add_tuple("function_has_implementation", vec![id.into()]);
}
}
impl trap::TrapClass for Function {
fn class_name() -> &'static str { "Function" }
}

View File

@@ -38,6 +38,7 @@ macro_rules! post_emit {
$self.extract_macro_call_expanded($node, $label);
};
(Function, $self:ident, $node:ident, $label:ident) => {
$self.emit_function_has_implementation($node, $label);
$self.extract_canonical_origin($node, $label.into());
};
(Trait, $self:ident, $node:ident, $label:ident) => {
@@ -83,6 +84,9 @@ 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)*) => {};
}
@@ -761,4 +765,24 @@ impl<'a> Translator<'a> {
generated::Item::emit_attribute_macro_expansion(label, expanded, &mut self.trap.writer);
}
}
pub(crate) fn emit_function_has_implementation(
&mut self,
node: &ast::Fn,
label: Label<generated::Function>,
) {
if node.body().is_some() {
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

@@ -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 03bd9bb84becc0716e12e8a788ab07098e568c58b43b63ed0d333b1c9e227ab7 3168e7b4cb551b9fde74967847576dada05f12a49a1b19c6900e0de32651bcd4
lib/codeql/rust/elements/internal/generated/Const.qll 56d954fe4f24a599e764ef926d6b42db8794c109b77851d1a5e81e0005d22610 30ac1d79bd4dbf84fe5d02b88ead0c1408e3b47254dc8f37703718ca81ebbefc
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
@@ -524,7 +524,7 @@ lib/codeql/rust/elements/internal/generated/FormatArgsArg.qll c762a4af8609472e28
lib/codeql/rust/elements/internal/generated/FormatArgsExpr.qll 8aed8715a27d3af3de56ded4610c6792a25216b1544eb7e57c8b0b37c14bd9c1 590a2b0063d2ecd00bbbd1ce29603c8fd69972e34e6daddf309c915ce4ec1375
lib/codeql/rust/elements/internal/generated/FormatArgument.qll cd05153276e63e689c95d5537fbc7d892615f62e110323759ef02e23a7587407 be2a4531b498f01625effa4c631d51ee8857698b00cfb829074120a0f2696d57
lib/codeql/rust/elements/internal/generated/FormatTemplateVariableAccess.qll a6175214fad445df9234b3ee9bf5147da75baf82473fb8d384b455e3add0dac1 a928db0ff126b2e54a18f5c488232abd1bd6c5eda24591d3c3bb80c6ee71c770
lib/codeql/rust/elements/internal/generated/Function.qll 6c04fffdc9de54cd01ff76f93aef5fcd3f2f779a2735523c9b1a859d394cefc9 af3c0f05c05ecd74560ab7b128a4a8e9822aa3cb80eddf304d51ea44725ac706
lib/codeql/rust/elements/internal/generated/Function.qll befc4220bef166531e52625b08642f129115ae918a70021d69874dc794e41be7 e6433f67000eb5f3e02b209d7ee8018fea30abed9e7c491fa1fbbd9d998e98ae
lib/codeql/rust/elements/internal/generated/GenericArg.qll 464da0ba1c5ddcd1be68617167f177773d99b5ac4775ec8ea24d503e789a9099 6faa1033d59baf7c210ac4837a55781cfc054b7acbad8027faf4630dbfa6e101
lib/codeql/rust/elements/internal/generated/GenericArgList.qll b8cd936bba6f28344e28c98acf38acb8ef43af6ecf8367d79ed487e5b9da17cb 8b14331261e49d004807285b02fca190aafd62bfb9378b05c7d9c1e95525fe7b
lib/codeql/rust/elements/internal/generated/GenericParam.qll a0285123f974f287154b706bf6688b86edf72a4adcec57346c654d962435651b b42c3915e9564b5b5c5282229bf882aa3309de26a77721b2255d6f4235c0cc38
@@ -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 e2c6aaaa1735113f160c0e178d682bff8e9ebc627632f73c0dd2d1f4f9d692a8 61cf70eb649f241e2fcd5e0ba34df63f3a14f07032811b9ae151721783a0fd20
lib/codeql/rust/elements/internal/generated/ParentChild.qll ca2dcc42bf0c9d004dd54d2b131d807fa4e97abbc8bb4d9176506efb15032bf7 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 de98fe8481864e23e1cd67d926ffd2e8bb8a83ed48901263122068f9c29ab372 3bd67fe283aaf24b94a2e3fd8f6e73ae34f61a097817900925d1cdcd3b745ecc
lib/codeql/rust/elements/internal/generated/Raw.qll cac788ae82503c953f6260665bf6be08c3dea1a3136186cac3e5242aa64abe13 10a41c862eb0bf8968665a3ce545f1effd38b5f97fec8a35b544592371da5771
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 6794d0056060a82258d1e832ad265e2eb276206f0224a3f0eb9221e225370066 0a6134fb5a849ce9bd1a28de783460301cafca5773bd7caa4fb1f774f81b476a
test/extractor-tests/generated/Const/Const.ql 7f9c9ce5b04391ff8f0d419a62972292b1d9811a978cceb129ef5a0f68a02fab c6c1dfa688248310cadd2d6d71d1a70a2b48f5b29ea2078f4dd51db59a0021c0
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
@@ -821,7 +821,7 @@ test/extractor-tests/generated/FormatArgsExpr/FormatTemplateVariableAccess.ql 27
test/extractor-tests/generated/FormatArgsExpr/Format_getArgumentRef.ql 634efdffaae4199aa9d95652cf081a8dc26e88224e24678845f8a67dc24ce090 d0302fee5c50403214771d5c6b896ba7c6e52be10c9bea59720ef2bb954e6f40
test/extractor-tests/generated/FormatArgsExpr/Format_getPrecisionArgument.ql 0d2140f84d0220b0c72c48c6bd272f4cfe1863d1797eddd16a6e238552a61e4d f4fe9b29697041e30764fa3dea44f125546bfb648f32c3474a1e922a4255c534
test/extractor-tests/generated/FormatArgsExpr/Format_getWidthArgument.ql 01ef27dd0bfab273e1ddc57ada0e079ece8a2bfd195ce413261006964b444093 acd0161f86010759417015c5b58044467a7f760f288ec4e8525458c54ae9a715
test/extractor-tests/generated/Function/Function.ql 2efae1916e8f501668b3dbb2237cda788243fdd643683eda41b108dfdc578a90 6ec948518963985ec41b66e2b3b2b953e1da872dcd052a6d8c8f61c25bf09600
test/extractor-tests/generated/Function/Function.ql 5be2478587433f79843b31ccd92e6982736e69736fa1c5ebc6e774ffcc0095ef 28a9091365c9e446e663525109c6d7d2442bba8294956d8c99c3928b32b0beea
test/extractor-tests/generated/Function/Function_getAbi.ql e5c9c97de036ddd51cae5d99d41847c35c6b2eabbbd145f4467cb501edc606d8 0b81511528bd0ef9e63b19edfc3cb638d8af43eb87d018fad69d6ef8f8221454
test/extractor-tests/generated/Function/Function_getAttr.ql 44067ee11bdec8e91774ff10de0704a8c5c1b60816d587378e86bf3d82e1f660 b4bebf9441bda1f2d1e34e9261e07a7468cbabf53cf8047384f3c8b11869f04e
test/extractor-tests/generated/Function/Function_getAttributeMacroExpansion.ql 17a346a9e5d28af99522520d1af3852db4cae01fb3d290a65c5f84d8d039c345 36fb06b55370828d9bc379cf5fad7f383cdb6f6db6f7377660276943ab0e1ec8

View File

@@ -106,5 +106,15 @@ 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

@@ -164,5 +164,15 @@ module Generated {
* Holds if `getWhereClause()` exists.
*/
final predicate hasWhereClause() { exists(this.getWhereClause()) }
/**
* Holds if this function 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::convertFunctionToRaw(this).(Raw::Function).hasImplementation()
}
}
}

View File

@@ -3350,6 +3350,14 @@ 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) }
}
/**
@@ -3530,6 +3538,14 @@ module Raw {
* Gets the where clause of this function, if it exists.
*/
WhereClause getWhereClause() { function_where_clauses(this, result) }
/**
* Holds if this function 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() { function_has_implementation(this) }
}
/**

View File

@@ -2816,6 +2816,11 @@ const_visibilities(
int visibility: @visibility ref
);
#keyset[id]
const_has_implementation(
int id: @const ref
);
enums(
unique int id: @enum
);
@@ -2985,6 +2990,11 @@ function_where_clauses(
int where_clause: @where_clause ref
);
#keyset[id]
function_has_implementation(
int id: @function ref
);
impls(
unique int id: @impl
);

View File

@@ -5,7 +5,8 @@ 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 isDefault, string hasName, string hasTypeRepr, string hasVisibility,
string hasImplementation
where
toBeTested(x) and
not x.isUnknown() and
@@ -26,8 +27,10 @@ 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"
(if x.hasVisibility() then hasVisibility = "yes" else hasVisibility = "no") and
if x.hasImplementation() then hasImplementation = "yes" else hasImplementation = "no"
select x, "hasExtendedCanonicalPath:", hasExtendedCanonicalPath, "hasCrateOrigin:", hasCrateOrigin,
"hasAttributeMacroExpansion:", hasAttributeMacroExpansion, "getNumberOfAttrs:", getNumberOfAttrs,
"hasBody:", hasBody, "isConst:", isConst, "isDefault:", isDefault, "hasName:", hasName,
"hasTypeRepr:", hasTypeRepr, "hasVisibility:", hasVisibility
"hasTypeRepr:", hasTypeRepr, "hasVisibility:", hasVisibility, "hasImplementation:",
hasImplementation

View File

@@ -6,7 +6,8 @@ from
Function x, string hasParamList, int getNumberOfAttrs, string hasExtendedCanonicalPath,
string hasCrateOrigin, string hasAttributeMacroExpansion, string hasAbi, string hasBody,
string hasGenericParamList, string isAsync, string isConst, string isDefault, string isGen,
string isUnsafe, string hasName, string hasRetType, string hasVisibility, string hasWhereClause
string isUnsafe, string hasName, string hasRetType, string hasVisibility, string hasWhereClause,
string hasImplementation
where
toBeTested(x) and
not x.isUnknown() and
@@ -34,10 +35,12 @@ where
(if x.hasName() then hasName = "yes" else hasName = "no") and
(if x.hasRetType() then hasRetType = "yes" else hasRetType = "no") and
(if x.hasVisibility() then hasVisibility = "yes" else hasVisibility = "no") and
if x.hasWhereClause() then hasWhereClause = "yes" else hasWhereClause = "no"
(if x.hasWhereClause() then hasWhereClause = "yes" else hasWhereClause = "no") and
if x.hasImplementation() then hasImplementation = "yes" else hasImplementation = "no"
select x, "hasParamList:", hasParamList, "getNumberOfAttrs:", getNumberOfAttrs,
"hasExtendedCanonicalPath:", hasExtendedCanonicalPath, "hasCrateOrigin:", hasCrateOrigin,
"hasAttributeMacroExpansion:", hasAttributeMacroExpansion, "hasAbi:", hasAbi, "hasBody:", hasBody,
"hasGenericParamList:", hasGenericParamList, "isAsync:", isAsync, "isConst:", isConst,
"isDefault:", isDefault, "isGen:", isGen, "isUnsafe:", isUnsafe, "hasName:", hasName,
"hasRetType:", hasRetType, "hasVisibility:", hasVisibility, "hasWhereClause:", hasWhereClause
"hasRetType:", hasRetType, "hasVisibility:", hasVisibility, "hasWhereClause:", hasWhereClause,
"hasImplementation:", hasImplementation

View File

@@ -951,6 +951,10 @@ 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)
@@ -1882,6 +1886,10 @@ class _:
class _:
param_list: drop
attrs: drop
has_implementation: predicate | doc("this function 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(ClosureExpr, add_bases=[Callable])