Anders Fugmann 7ea8558c25 Kotlin: converge delegated-property accessor synthetic thisRef locations onto K2
For `val x by Delegate()`, the compiler synthesises getter/setter bodies that
forward to the delegate's `getValue`/`setValue`, passing a synthetic receiver
argument (the enclosing `this`, or `null` for top-level/extension delegates).

Under the K1 frontend (test-kotlin1, `-language-version 1.9`) these synthetic
receiver arguments were given a bogus location `1:9:1:12`: their IR offsets
(8..11) are not real source offsets for the argument, and `findPsiElement`
resolves them to whatever sits at file offset 8, which for these tests is the
line-1 `import`. This is meaningless and makes the receivers un-searchable by
location.

The K2 frontend (test-kotlin2, default) already produces the intuitive result:
the synthetic member receiver is located at the DELEGATE EXPRESSION (e.g.
`by ResourceDelegate()` -> `ResourceDelegate()`), and a receiver-less `null`
argument gets the whole-file location `0:0:0:0`. K2 output is the canonical
target for this unification (it is also compiler-version-independent).

This change adds `getDelegatedAccessorSyntheticArgumentLocation`, gated on:
  - the enclosing declaration being an IrFunction with origin
    DELEGATED_PROPERTY_ACCESSOR (member/top-level accessors only),
  - the element being either the accessor's dispatch/extension receiver
    `IrGetValue` or a null `CodeQLIrConst`, and
  - the element offsets lying outside the enclosing `KtProperty` text range
    (so genuine in-source expressions are never rehomed).
It returns the delegate expression's location for the `this`/receiver case and
the whole-file location for the `null` case, mirroring K2. It is wired at the
three sites that extract these synthetic args (extractThisAccess, the
IrGetValue variable/extension-receiver path, and the null case of
extractConstant).

The helper relies on PSI (getPsi2Ir), so it is a no-op under K2: test-kotlin2
output is unchanged (verified). Only test-kotlin1 converges.

Trade-offs / scope:
  - LOCAL delegated properties (declared inside a function body) are a distinct
    mechanism: their get/set is inlined into the enclosing function (no
    DELEGATED_PROPERTY_ACCESSOR origin) and the residual divergence there is an
    end-offset difference, not the `1:9:1:12` bug. They are deliberately left
    for a separate commit.
  - The `$delegate` backing-variable initializer/type-access spans still differ
    between suites (e.g. `4:18` vs `4:21`); that is an unrelated mechanism and
    is not touched here.
  - The outside-KtProperty-range gate could in principle false-negative for a
    delegated property declared at the very top of a file (offsets <= 11); this
    is preferred to over-correcting real expressions and is not observed in the
    test corpus.

Relearned test-kotlin1 expected updated (exprs, methods; incl. PrintAst).
Verified via full dual-suite relearn (CI-faithful: 2.3.20/lang-1.9 for tk1,
default/2.4.0 for tk2, database consistency checks): all 3333 tests pass, only
the three delegates-related tk1 files change, every changed row is a pure
relocation of a synthetic receiver from `1:9:1:12` to the K2 target, and
test-kotlin2 is byte-for-byte unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-07-12 00:31:25 +02:00
2022-10-20 08:21:02 -04:00
2026-05-27 17:41:44 +02:00
2026-07-09 03:03:43 +00:00
2026-06-29 12:05:42 +02:00
2026-02-10 13:44:04 +01:00
2018-09-23 16:24:31 -07:00
2025-09-25 14:03:39 +01:00
2026-05-12 11:24:35 +00:00
2026-05-08 13:41:45 +00:00
2022-04-12 12:40:59 +02:00
2024-05-07 13:09:08 +01:00

CodeQL

This open source repository contains the standard CodeQL libraries and queries that power GitHub Advanced Security and the other application security products that GitHub makes available to its customers worldwide.

How do I learn CodeQL and run queries?

There is extensive documentation about the CodeQL language, writing CodeQL using the CodeQL extension for Visual Studio Code and using the CodeQL CLI.

Contributing

We welcome contributions to our standard library and standard checks. Do you have an idea for a new check, or how to improve an existing query? Then please go ahead and open a pull request! Before you do, though, please take the time to read our contributing guidelines. You can also consult our style guides to learn how to format your code for consistency and clarity, how to write query metadata, and how to write query help documentation for your query.

For information on contributing to CodeQL documentation, see the "contributing guide" for docs.

License

The code in this repository is licensed under the MIT License by GitHub.

The CodeQL CLI (including the CodeQL engine) is hosted in a different repository and is licensed separately. If you'd like to use the CodeQL CLI to analyze closed-source code, you will need a separate commercial license; please contact us for further help.

Visual Studio Code integration

If you use Visual Studio Code to work in this repository, there are a few integration features to make development easier.

CodeQL for Visual Studio Code

You can install the CodeQL for Visual Studio Code extension to get syntax highlighting, IntelliSense, and code navigation for the QL language, as well as unit test support for testing CodeQL libraries and queries.

Tasks

The .vscode/tasks.json file defines custom tasks specific to working in this repository. To invoke one of these tasks, select the Terminal | Run Task... menu option, and then select the desired task from the dropdown. You can also invoke the Tasks: Run Task command from the command palette.

Description
CodeQL: the libraries and queries that power security researchers around the world, as well as code scanning in GitHub Advanced Security
Readme MIT 20 GiB
Languages
CodeQL 32.2%
Kotlin 27.1%
C# 16.9%
Java 7.6%
Python 4.6%
Other 11.3%