Skip to content

Commit 02f4735

Browse files
committed
Merge branch 'topic/rules/fix_unconstrained_array_returns' into 'master'
Fix the "unconstrained_array_returns" rule Closes #495 See merge request eng/libadalang/langkit-query-language!458
2 parents 549a244 + 2c9a536 commit 02f4735

File tree

6 files changed

+84
-25
lines changed

6 files changed

+84
-25
lines changed
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
import stdlib
22

3-
fun unconstrained_array_type(type, except_string) =
4-
if type is SubtypeDecl
5-
then type.f_subtype.f_constraint is not Constraint and
6-
unconstrained_array_type(type.p_canonical_type(), except_string)
7-
else if type is TypeDecl(f_type_def: DerivedTypeDef)
8-
then type.f_type_def.f_subtype_indication.f_constraint is not Constraint and
9-
unconstrained_array_type(type.p_base_type(), except_string)
10-
else type is TypeDecl(p_is_array_type(): true,
11-
f_type_def: ArrayTypeDef(f_indices:
12-
UnconstrainedArrayIndices))
13-
when not (except_string and
14-
type.p_root_type().p_fully_qualified_name()
15-
== "Standard.String")
16-
173
@check(message="function returns unconstrained array",
184
follow_generic_instantiations=true, category="Feature")
195
fun unconstrained_array_returns(node, except_string = false) =
206
|" Flag each function returning an unconstrained array. Function declarations,
217
|" function bodies (and body stubs) having no separate specifications,
228
|" and generic function instantiations are flagged.
23-
|" Function calls and function renamings are
24-
|" not flagged.
9+
|" Function calls and function renamings are not flagged.
2510
|"
2611
|" Generic function declarations, and function declarations in generic
2712
|" packages, are not flagged. Instead, this rule flags the results of
@@ -45,13 +30,31 @@ fun unconstrained_array_returns(node, except_string = false) =
4530
|"
4631
|" function F1 (I : Integer) return Arr; -- FLAG
4732
|" function F2 (I : Integer) return Arr_S;
48-
node is (AbstractSubpDecl | SubpDecl | GenericSubpInternal |
49-
BaseSubpBody(p_previous_part(): null) |
50-
SubpBodyStub(p_previous_part(): null))
51-
52-
when (not stdlib.in_generic_template(node))
33+
{
34+
fun is_unconstrained_array(type_decl) =
35+
|" Get whether the provided TypeDecl is declaring an unconstrained array type.
36+
|" If `type_decl` designate the "standard.string" type and `except_string` is
37+
|" `true`, the result of this function is `false`.
38+
type_decl is TypeDecl(
39+
p_is_array_type(): true,
40+
f_type_def: ArrayTypeDef(f_indices: UnconstrainedArrayIndices)
41+
) when (
42+
if except_string
43+
then type_decl.p_fully_qualified_name().to_lower_case != "standard.string"
44+
);
5345

54-
and node.f_subp_spec is SubpSpec(f_subp_returns:
55-
TypeExpr(p_designated_type_decl():
56-
t@BaseTypeDecl
57-
when unconstrained_array_type(t, except_string)))
46+
node is (
47+
AbstractSubpDecl |
48+
SubpDecl |
49+
GenericSubpInternal |
50+
BaseSubpBody(p_previous_part(): null) |
51+
SubpBodyStub(p_previous_part(): null)
52+
)
53+
and (not stdlib.in_generic_template(node))
54+
and node.f_subp_spec is SubpSpec(
55+
f_subp_returns: TypeExpr(
56+
p_is_definite_subtype(): false,
57+
p_designated_type_decl(): t@BaseTypeDecl when is_unconstrained_array(t.p_root_type())
58+
)
59+
)
60+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Derived is
2+
3+
type Constrained is new String (1 .. 6);
4+
type D_Constrained is new Constrained;
5+
6+
type Unconstrained is new String;
7+
type D_Unconstrained is new Unconstrained;
8+
9+
function Get_Constrained return Constrained is ("123456"); -- NOFLAG
10+
function Get_D_Constrained return D_Constrained is ("123456"); -- NOFLAG
11+
12+
function Get_Unconstrained return Unconstrained is (""); -- FLAG
13+
function Get_D_Unconstrained return D_Unconstrained is (""); -- FLAG
14+
15+
end Derived;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
separate (Stub)
2+
function Subp1 (I : Integer) return String is -- NOFLAG
3+
begin
4+
return "";
5+
end Subp1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
separate (Stub)
2+
function Subp2 (I : Integer) return String is -- NOFLAG
3+
begin
4+
return "";
5+
end Subp2;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package Subtypes is
2+
3+
subtype Constrained is String (1 .. 6);
4+
subtype S_Constrained is Constrained;
5+
6+
subtype Unconstrained is String;
7+
subtype S_Unconstrained is Unconstrained;
8+
9+
function Get_Constrained return Constrained is ("123456"); -- NOFLAG
10+
function Get_S_Constrained return S_Constrained is ("123456"); -- NOFLAG
11+
12+
function Get_Unconstrained return Unconstrained is (""); -- FLAG
13+
function Get_S_Unconstrained return S_Unconstrained is (""); -- FLAG
14+
15+
end Subtypes;

testsuite/tests/checks/unconstrained_array_returns/test.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
derived.ads:12:13: rule violation: function returns unconstrained array
2+
12 | function Get_Unconstrained return Unconstrained is (""); -- FLAG
3+
| ^^^^^^^^^^^^^^^^^
4+
5+
derived.ads:13:13: rule violation: function returns unconstrained array
6+
13 | function Get_D_Unconstrained return D_Unconstrained is (""); -- FLAG
7+
| ^^^^^^^^^^^^^^^^^^^
8+
19
generics.ads:6:13: rule violation: function returns unconstrained array
210
6 | function Image (X : Integer) return String; -- FLAG
311
| ^^^^^
@@ -38,3 +46,11 @@ stub.adb:8:13: rule violation: function returns unconstrained array
3846
8 | function Subp2 (I : Integer) return String is separate; -- FLAG
3947
| ^^^^^
4048

49+
subtypes.ads:12:13: rule violation: function returns unconstrained array
50+
12 | function Get_Unconstrained return Unconstrained is (""); -- FLAG
51+
| ^^^^^^^^^^^^^^^^^
52+
53+
subtypes.ads:13:13: rule violation: function returns unconstrained array
54+
13 | function Get_S_Unconstrained return S_Unconstrained is (""); -- FLAG
55+
| ^^^^^^^^^^^^^^^^^^^
56+

0 commit comments

Comments
 (0)