Implement support for constant expressions
@cirras がすでに取り組んでいます。
2025年5月28日 から。
評価
この issue はまだ評価されていません。
説明
Prerequisites
- This improvement has not already been suggested.
- This improvement would be generally useful, not specific to my code or setup.
Engine area
Delphi language support
Improvement description
Background
The Delphi compiler implements the following optimizations around constant expressions:
- constant folding
- constant propagation
This has significant implications for type resolution - see example.
Example
// overload for each integer type:
procedure X(B: Byte); overload; begin WriteLn('Byte'); end;
procedure X(S: ShortInt); overload; begin WriteLn('ShortInt'); end;
procedure X(S: SmallInt); overload; begin WriteLn('SmallInt'); end;
procedure X(W: Word); overload; begin WriteLn('Word'); end;
procedure X(I: Integer); overload; begin WriteLn('Integer'); end;
procedure X(C: Cardinal); overload; begin WriteLn('Cardinal'); end;
procedure X(I: Int64); overload; begin WriteLn('Int64'); end;
procedure X(U: UInt64); overload; begin WriteLn('UInt64'); end;
const
CFive = 5; // technically ShortInt, but the value "5" is propagated to constant expressions
CHundred = 25 * 4; // technically ShortInt, but the value "100" is propagated to constant expressions
procedure Test;
var
I8: ShortInt;
I16: SmallInt;
I32: Integer;
I64: Int64;
U8: Byte;
U16: Word;
U32: Cardinal;
U64: UInt64;
begin
// For variables, all we need to do to lose the original type is tack on a unary `+`.
// This is because the expression is evaluated at runtime, and only the following options are available at runtime:
//
// Positive(Integer): Integer
// Positive(Cardinal): Cardinal
// Positive(Int64): Int64
// Positive(UInt64): UInt64
X(+I8); // Integer
X(+I16); // Integer
X(+I32); // Integer
X(+I64); // Int64
X(+U8); // Integer
X(+U16); // Integer
X(+U32); // Cardinal
X(+U64); // UInt64
// But for constant expressions, that unary `+` (and indeed, all other arithmetic operators) are done at compile-time.
// The type is resolved *after* the expression is evaluated, based on the value.
X(+(0)); // ShortInt
X(+(128)); // Byte
X(+(256)); // SmallInt
X(+(32768)); // Word
X(+(65536)); // Integer
X(+(2147483648)); // Cardinal
X(+(4294967296)); // Int64
X(+(9223372036854775808)); // UInt64
// The following constant expressions feature constant propagation.
X(+CFive); // ShortInt
X(+CHundred); // ShortInt
X(+(CHundred + CHundred)); // Byte
X(+(256 - CFive)); // Byte
end;
See Also
Rationale
This is required for fully accurate type resolution.
Known affected areas
- Type conversions from array constructor -> set
- Expression type resolution (especially unary
-) - Subrange type modeling (low and high are constant expressions)
Other benefits
- Having access to evaluated constant expressions would be extremely useful for some rules.
- Really, any rule where we might want to raise an issue based on the contents of a string
- see: https://github.com/integrated-application-development/sonar-delphi/issues/94#issuecomment-1825031266
- 主要言語
- Java
- スター
- 160
- フォーク
- 36
- 平均マージ
- 22日 7時間
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
integrated-application-development/sonar-delphi のほかの issue
-
bug triage
難易度 3/5 1〜2日 初心者へのやさしさ 66/100
integrated-application-development/sonar-delphi#473 · コメント 3 件 ·
-
VariableInitialization false positive for custom managed record with Initialize operator対応中かも @Bazooper-blip が 37 日前に担当しました。 オープンenhancement rule
難易度 3/5 1〜2日 初心者へのやさしさ 65/100
integrated-application-development/sonar-delphi#468 · コメント 1 件 ·
-
engine enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
feature rule
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
-
feature rule
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
integrated-application-development/sonar-delphi#423 · コメント 1 件 ·
integrated-application-development/sonar-delphi の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
quarkusio/quarkus-agent-mcp#302 ·
メンテナーはふだん 2 日以内に返信
-
build
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
MetricsHub/metricshub-community#1472 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
IdentityArrayObjectSet iterator remove may throw ArrayIndexOutOfBoundsException対応中かも @jiteshkhatri11 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
javapathfinder/jpf-core#660 · コメント 1 件 ·