diff --git a/ada/ast.py b/ada/ast.py index 9c12d9753..008755683 100644 --- a/ada/ast.py +++ b/ada/ast.py @@ -1874,6 +1874,25 @@ class BasicDecl(AdaNode): associates a name with a language entity, for example a type or a variable. """ + @langkit_property(return_type=Equation, dynamic_vars=[origin]) + def subp_constrain_prefix(prefix=T.Expr): + """ + Implementation for ``BasicDecl.constrain_prefix`` but for subprograms. + + Since subprograms can't have a base class, this is shared here. + """ + return If( + # If self is a dottable subprogram, then we want to constrain the + # prefix so that it's type is the type of the first parameter of + # self. + Entity.info.md.dottable_subp, + Bind(prefix.expected_type_var, + Entity.subp_spec_or_null + .unpacked_formal_params.at(0)._.formal_decl.formal_type) + & prefix.matches_expected_prefix_type, + LogicTrue() + ) + @langkit_property() def env_hook_basic_decl(): """ @@ -9771,17 +9790,7 @@ def next_part_for_decl(): @langkit_property() def constrain_prefix(prefix=T.Expr): - return If( - # If self is a dottable subprogram, then we want to constrain the - # prefix so that it's type is the type of the first parameter of - # self. - Entity.info.md.dottable_subp, - Bind(prefix.expected_type_var, - Entity.subp_decl_spec - .unpacked_formal_params.at(0)._.formal_decl.formal_type) - & prefix.matches_expected_prefix_type, - LogicTrue() - ) + return Entity.subp_constrain_prefix(prefix) @langkit_property() def expr_type(): @@ -20129,6 +20138,10 @@ class BaseSubpBody(Body): defining_names = Property(Entity.subp_spec.name.singleton) + @langkit_property() + def constrain_prefix(prefix=T.Expr): + return Entity.subp_constrain_prefix(prefix) + @langkit_property() def defining_env(): return If( diff --git a/testsuite/tests/name_resolution/chained_dot_calls/test.out b/testsuite/tests/name_resolution/chained_dot_calls/test.out index 98cca609d..3c3d3e198 100644 --- a/testsuite/tests/name_resolution/chained_dot_calls/test.out +++ b/testsuite/tests/name_resolution/chained_dot_calls/test.out @@ -18,11 +18,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -30,11 +30,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -42,11 +42,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -54,7 +54,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -62,11 +62,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -74,7 +74,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -82,11 +82,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -94,7 +94,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -102,11 +102,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -114,7 +114,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -122,11 +122,11 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -134,7 +134,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None @@ -142,7 +142,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -154,7 +154,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -199,7 +199,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -244,7 +244,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -344,7 +344,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -422,7 +422,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -434,7 +434,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: @@ -446,7 +446,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: diff --git a/testsuite/tests/name_resolution/classwide_1/test.out b/testsuite/tests/name_resolution/classwide_1/test.out index 5d43e504d..004f66add 100644 --- a/testsuite/tests/name_resolution/classwide_1/test.out +++ b/testsuite/tests/name_resolution/classwide_1/test.out @@ -46,7 +46,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.adb b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.adb new file mode 100644 index 000000000..74755f288 --- /dev/null +++ b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.adb @@ -0,0 +1,21 @@ +procedure Test is + package Pkg1 is + type T is tagged null record; + + function Foo (X : T) return Integer is (1); + end Pkg1; + + package Pkg2 is + type T is tagged null record; + + function Foo (X : T) return Integer is (2); + end Pkg2; + + function Bar (X : Integer) return Pkg1.T is (null record); + function Bar (Y : Boolean) return Pkg2.T is (null record); + + X : Integer; +begin + X := Bar (1).Foo; + pragma Test_Statement; +end Test; diff --git a/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.out b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.out new file mode 100644 index 000000000..06f980d0f --- /dev/null +++ b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.out @@ -0,0 +1,33 @@ +Analyzing test.adb +################## + +Resolving xrefs for node +********************************************************* + +Expr: + references: + type: + expected type: None +Expr: + references: + type: + expected type: +Expr: + references: + type: + expected type: +Expr: + references: + type: + expected type: None +Expr: + references: None + type: + expected type: +Expr: + references: + type: + expected type: + + +Done. diff --git a/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.yaml b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.yaml new file mode 100644 index 000000000..173e325ff --- /dev/null +++ b/testsuite/tests/name_resolution/constrain_prefix_subp_body/test.yaml @@ -0,0 +1,2 @@ +driver: name-resolution +input_sources: [test.adb] diff --git a/testsuite/tests/name_resolution/dotted_call/test.out b/testsuite/tests/name_resolution/dotted_call/test.out index 1830ec877..ede04fc6a 100644 --- a/testsuite/tests/name_resolution/dotted_call/test.out +++ b/testsuite/tests/name_resolution/dotted_call/test.out @@ -71,7 +71,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None @@ -87,7 +87,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None @@ -103,7 +103,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/field_hiding/test.out b/testsuite/tests/name_resolution/field_hiding/test.out index eb931469c..fa0743a2c 100644 --- a/testsuite/tests/name_resolution/field_hiding/test.out +++ b/testsuite/tests/name_resolution/field_hiding/test.out @@ -51,7 +51,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: diff --git a/testsuite/tests/name_resolution/interfaces/test.out b/testsuite/tests/name_resolution/interfaces/test.out index 1b627504c..368664926 100644 --- a/testsuite/tests/name_resolution/interfaces/test.out +++ b/testsuite/tests/name_resolution/interfaces/test.out @@ -103,7 +103,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/pre_cond_type_view/test.out b/testsuite/tests/name_resolution/pre_cond_type_view/test.out index 186e7120f..c97711772 100644 --- a/testsuite/tests/name_resolution/pre_cond_type_view/test.out +++ b/testsuite/tests/name_resolution/pre_cond_type_view/test.out @@ -31,7 +31,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: diff --git a/testsuite/tests/name_resolution/precise_override/test.out b/testsuite/tests/name_resolution/precise_override/test.out index 732916b57..95a3b4bc9 100644 --- a/testsuite/tests/name_resolution/precise_override/test.out +++ b/testsuite/tests/name_resolution/precise_override/test.out @@ -11,7 +11,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/primitive_in_different_scope/test.out b/testsuite/tests/name_resolution/primitive_in_different_scope/test.out index 59d44f395..b8140ebbe 100644 --- a/testsuite/tests/name_resolution/primitive_in_different_scope/test.out +++ b/testsuite/tests/name_resolution/primitive_in_different_scope/test.out @@ -30,7 +30,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/primitive_on_incomplete/test.out b/testsuite/tests/name_resolution/primitive_on_incomplete/test.out index 61a403ea3..6bebd37e8 100644 --- a/testsuite/tests/name_resolution/primitive_on_incomplete/test.out +++ b/testsuite/tests/name_resolution/primitive_on_incomplete/test.out @@ -27,7 +27,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/primitive_through_interface/test.out b/testsuite/tests/name_resolution/primitive_through_interface/test.out index 2583ccaa3..3bfdebad2 100644 --- a/testsuite/tests/name_resolution/primitive_through_interface/test.out +++ b/testsuite/tests/name_resolution/primitive_through_interface/test.out @@ -59,7 +59,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/primitive_through_private/test.out b/testsuite/tests/name_resolution/primitive_through_private/test.out index cc60ef3b3..79ea5f89f 100644 --- a/testsuite/tests/name_resolution/primitive_through_private/test.out +++ b/testsuite/tests/name_resolution/primitive_through_private/test.out @@ -59,7 +59,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/subp_visibility_order/test.out b/testsuite/tests/name_resolution/subp_visibility_order/test.out index c36d02dc0..2ad5c26e3 100644 --- a/testsuite/tests/name_resolution/subp_visibility_order/test.out +++ b/testsuite/tests/name_resolution/subp_visibility_order/test.out @@ -27,7 +27,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/testsuite/tests/name_resolution/xref_eq_tagged_primitives/test.out b/testsuite/tests/name_resolution/xref_eq_tagged_primitives/test.out index 6175e4e5a..756fe9e3c 100644 --- a/testsuite/tests/name_resolution/xref_eq_tagged_primitives/test.out +++ b/testsuite/tests/name_resolution/xref_eq_tagged_primitives/test.out @@ -16,7 +16,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None @@ -56,7 +56,7 @@ Expr: Expr: references: type: - expected type: None + expected type: Expr: references: type: None diff --git a/user_manual/changes/libadalang/953.yaml b/user_manual/changes/libadalang/953.yaml new file mode 100644 index 000000000..2883a6301 --- /dev/null +++ b/user_manual/changes/libadalang/953.yaml @@ -0,0 +1,8 @@ +type: bugfix +title: Fix nameres of dot-calls when target is a subp body +description: | + There was missing constraints in name resolution that could lead dotted + calls to mis-resolve if they were referencing subprogram bodies rather than + subprogram declarations. This is now fixed. + +date: 2023-04-03