-
Notifications
You must be signed in to change notification settings - Fork 802
Add semantic analysis and negative tests for natural expressions #43993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1527cbf
64e284d
8b8acc9
72d03b5
2264928
c8e9b5b
166824b
95980d6
88c9c44
73daf87
736e0d2
4748b59
7726ada
7af41c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,81 @@ | ||||||
| /* | ||||||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org). | ||||||
| * | ||||||
| * WSO2 Inc. licenses this file to you under the Apache License, | ||||||
MaryamZi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| * Version 2.0 (the "License"); you may not use this file except | ||||||
| * in compliance with the License. | ||||||
| * You may obtain a copy of the License at | ||||||
| * | ||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| * | ||||||
| * Unless required by applicable law or agreed to in writing, | ||||||
| * software distributed under the License is distributed on an | ||||||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||
| * KIND, either express or implied. See the License for the | ||||||
| * specific language governing permissions and limitations | ||||||
| * under the License. | ||||||
| */ | ||||||
|
|
||||||
| package io.ballerina.projects.internal; | ||||||
|
|
||||||
| import io.ballerina.compiler.syntax.tree.ExternalFunctionBodyNode; | ||||||
| import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; | ||||||
| import io.ballerina.compiler.syntax.tree.ModulePartNode; | ||||||
| import io.ballerina.compiler.syntax.tree.NaturalExpressionNode; | ||||||
| import io.ballerina.compiler.syntax.tree.Node; | ||||||
| import io.ballerina.compiler.syntax.tree.NodeVisitor; | ||||||
| import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode; | ||||||
|
|
||||||
| /** | ||||||
| * Check and add an import for the natural programming module if there is a natural expression. | ||||||
| * | ||||||
| * @since 2201.13.0 | ||||||
| */ | ||||||
| public class NaturalProgrammingImportAnalyzer extends NodeVisitor { | ||||||
|
|
||||||
| private boolean shouldImportNaturalProgrammingModule; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in #44013. |
||||||
| private static final String CODE_ANNOTATION = "code"; | ||||||
|
|
||||||
| public boolean shouldImportNaturalProgrammingModule(ModulePartNode modulePartNode) { | ||||||
| modulePartNode.accept(this); | ||||||
| return this.shouldImportNaturalProgrammingModule; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void visit(NaturalExpressionNode naturalExpressionNode) { | ||||||
| this.shouldImportNaturalProgrammingModule = true; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void visit(FunctionDefinitionNode functionDefinitionNode) { | ||||||
| if (this.shouldImportNaturalProgrammingModule) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (isExternalFunctionWithCodeAnnotation(functionDefinitionNode)) { | ||||||
| this.shouldImportNaturalProgrammingModule = true; | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| super.visit(functionDefinitionNode); | ||||||
| } | ||||||
|
|
||||||
| private boolean isExternalFunctionWithCodeAnnotation(FunctionDefinitionNode functionDefinitionNode) { | ||||||
| if (!(functionDefinitionNode.functionBody() instanceof ExternalFunctionBodyNode externalFunctionBodyNode)) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| return externalFunctionBodyNode.annotations().stream() | ||||||
| .anyMatch(annotation -> | ||||||
| annotation.annotReference() instanceof SimpleNameReferenceNode annotReference && | ||||||
| CODE_ANNOTATION.equals(annotReference.name().text())); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since code is a common name, shall we check the prompt field also?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll have the module prefix then.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we don't give a redeclared symbol error for code in the current module. Will fix this for m2.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is when there is a |
||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| protected void visitSyntaxNode(Node node) { | ||||||
| if (this.shouldImportNaturalProgrammingModule) { | ||||||
| return; | ||||||
| } | ||||||
| super.visitSyntaxNode(node); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org). | ||
| * | ||
| * WSO2 Inc. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.ballerinalang.model.tree.expressions; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Represents a natural expression node. | ||
| * | ||
| * @since 2201.13.0 | ||
| */ | ||
| public interface NaturalExpressionNode extends ExpressionNode { | ||
|
|
||
| List<? extends ExpressionNode> getInsertions(); | ||
|
|
||
| void addInsertion(ExpressionNode expression); | ||
|
|
||
| List<? extends ExpressionNode> getStrings(); | ||
|
|
||
| void addString(ExpressionNode stringLiteral); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -817,7 +817,17 @@ public enum DiagnosticErrorCode implements DiagnosticCode { | |
| EXPRESSION_OF_FUTURE_TYPE_EXPECTED("BCE4057", "future.expression.expected"), | ||
| INSTANTIATION_ERROR("BCE4058", "instantiation.error"), | ||
| INVALID_BINDING_PATTERN_IN_ON_FAIL("BCE4059", "invalid.binding.pattern.in.on.fail"), | ||
| INVALID_USAGE_OF_CHECK_IN_PARAMETER_DEFAULT("BCE4060", "invalid.usage.of.check.in.parameter.default") | ||
| INVALID_USAGE_OF_CHECK_IN_PARAMETER_DEFAULT("BCE4060", "invalid.usage.of.check.in.parameter.default"), | ||
|
|
||
| EXPECTED_TYPE_FOR_NATURAL_EXPR_MUST_CONTAIN_ERROR("BCE4070", "expected.type.for.natural.expr.must.contain.error"), | ||
| EXPECTED_TYPE_FOR_NATURAL_EXPR_MUST_CONTAIN_A_UNION_OF_NON_ERROR_AND_ERROR( | ||
| "BCE4071", "expected.type.for.natural.expr.must.contain.a.union.of.non.error.and.error"), | ||
| EXPECTED_TYPE_FOR_NATURAL_EXPR_MUST_BE_A_SUBTYPE_OF_ANYDATA_OR_ERROR( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dont we need to avoid the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why though? |
||
| "BCE4072", "expected.type.for.natural.expr.must.be.a.subtype.of.anydata.or.error"), | ||
| CONST_NATURAL_EXPR_CAN_HAVE_ONLY_CONST_EXPR_INSERTION( | ||
| "BCE4073", "const.natural.expr.can.have.only.const.expr.insertion"), | ||
| EXPECTED_TYPE_FOR_CONST_NATURAL_EXPR_MUST_BE_A_SUBTYPE_OF_ANYDATA( | ||
| "BCE4074", "expected.type.for.const.natural.expr.must.be.a.subtype.of.anydata") | ||
| ; | ||
|
|
||
| private final String diagnosticId; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In here Don't we need to consider about the language version
As an example np module can have different versions for U12 and U13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's resolved by the project API? Which is why we shouldn't specify a version here?