-
Notifications
You must be signed in to change notification settings - Fork 744
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
Fix several issues with object type reference #18404
Merged
hasithaa
merged 6 commits into
ballerina-platform:master
from
MaryamZi:obj-reference-fixes
Sep 2, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
27f14ac
Disallow referncing two objects with functions with the same name
MaryamZi ec8d952
Consider param visibilty for function exact match check
MaryamZi 38e3423
Validate object type refs across modules and copy funcs only for vali…
MaryamZi b4f7b85
Merge branch 'master' of https://github.com/ballerina-lang/ballerina …
MaryamZi 3356dfe
Disallow fields with default values in abstract objects
MaryamZi ccf5e3d
Fix tests
MaryamZi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
import org.wso2.ballerinalang.compiler.semantics.model.types.BField; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BServiceType; | ||
import org.wso2.ballerinalang.compiler.semantics.model.types.BStructureType; | ||
|
@@ -1284,6 +1285,7 @@ private void defineMembers(List<BLangTypeDefinition> typeDefNodes, SymbolEnv pkg | |
defineNode(f, objMethodsEnv); | ||
}); | ||
|
||
List<String> referencedFunctions = new ArrayList<>(); | ||
// Add the attached functions of the referenced types to this object. | ||
// Here it is assumed that all the attached functions of the referred type are | ||
// resolved by the time we reach here. It is achieved by ordering the typeDefs | ||
|
@@ -1295,7 +1297,7 @@ private void defineMembers(List<BLangTypeDefinition> typeDefNodes, SymbolEnv pkg | |
|
||
List<BAttachedFunction> functions = ((BObjectTypeSymbol) typeRef.type.tsymbol).attachedFuncs; | ||
for (BAttachedFunction function : functions) { | ||
defineReferencedFunction(typeDef, objMethodsEnv, typeRef, function); | ||
defineReferencedFunction(typeDef, objMethodsEnv, typeRef, function, referencedFunctions); | ||
} | ||
} | ||
} else if (typeDef.symbol.kind == SymbolKind.RECORD) { | ||
|
@@ -1688,6 +1690,7 @@ private void createDummyTypeDefSymbol(TypeDefinition typeDefNode, SymbolEnv env) | |
|
||
private void resolveReferencedFields(BLangStructureTypeNode structureTypeNode, SymbolEnv typeDefEnv) { | ||
List<BSymbol> referencedTypes = new ArrayList<>(); | ||
List<BLangType> invalidTypeRefs = new ArrayList<>(); | ||
// Get the inherited fields from the type references | ||
structureTypeNode.referencedFields = structureTypeNode.typeRefs.stream().flatMap(typeRef -> { | ||
BType referredType = symResolver.resolveTypeNode(typeRef, typeDefEnv); | ||
|
@@ -1698,13 +1701,28 @@ private void resolveReferencedFields(BLangStructureTypeNode structureTypeNode, S | |
// Check for duplicate type references | ||
if (referencedTypes.contains(referredType.tsymbol)) { | ||
dlog.error(typeRef.pos, DiagnosticCode.REDECLARED_TYPE_REFERENCE, typeRef); | ||
invalidTypeRefs.add(typeRef); | ||
return Stream.empty(); | ||
} | ||
|
||
if (structureTypeNode.type.tag == TypeTags.OBJECT && (referredType.tag != TypeTags.OBJECT || !Symbols | ||
.isFlagOn(referredType.tsymbol.flags, Flags.ABSTRACT))) { | ||
dlog.error(typeRef.pos, DiagnosticCode.INCOMPATIBLE_TYPE_REFERENCE, typeRef); | ||
return Stream.empty(); | ||
if (structureTypeNode.type.tag == TypeTags.OBJECT) { | ||
if (referredType.tag != TypeTags.OBJECT || | ||
!Symbols.isFlagOn(referredType.tsymbol.flags, Flags.ABSTRACT)) { | ||
dlog.error(typeRef.pos, DiagnosticCode.INCOMPATIBLE_TYPE_REFERENCE, typeRef); | ||
invalidTypeRefs.add(typeRef); | ||
return Stream.empty(); | ||
} | ||
|
||
BObjectType objectType = (BObjectType) referredType; | ||
if (structureTypeNode.type.tsymbol.owner != referredType.tsymbol.owner) { | ||
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. There can be scenario, this logic fails, Like, local type def. Better to validate pkg, instated. |
||
if (objectType.fields.stream().anyMatch(field -> !Symbols.isPublic(field.symbol)) || | ||
((BObjectTypeSymbol) objectType.tsymbol).attachedFuncs.stream() | ||
.anyMatch(func -> !Symbols.isPublic(func.symbol))) { | ||
dlog.error(typeRef.pos, DiagnosticCode.INCOMPATIBLE_TYPE_REFERENCE_NON_PUBLIC_MEMBERS, typeRef); | ||
invalidTypeRefs.add(typeRef); | ||
return Stream.empty(); | ||
} | ||
} | ||
} | ||
|
||
if (structureTypeNode.type.tag == TypeTags.RECORD && referredType.tag != TypeTags.RECORD) { | ||
|
@@ -1724,15 +1742,23 @@ private void resolveReferencedFields(BLangStructureTypeNode structureTypeNode, S | |
return var; | ||
}); | ||
}).collect(Collectors.toList()); | ||
structureTypeNode.typeRefs.removeAll(invalidTypeRefs); | ||
} | ||
|
||
private void defineReferencedFunction(BLangTypeDefinition typeDef, SymbolEnv objEnv, BLangType typeRef, | ||
BAttachedFunction referencedFunc) { | ||
BAttachedFunction referencedFunc, List<String> referencedFunctions) { | ||
String referencedFuncName = referencedFunc.funcName.value; | ||
Name funcName = names.fromString( | ||
Symbols.getAttachedFuncSymbolName(typeDef.symbol.name.value, referencedFunc.funcName.value)); | ||
Symbols.getAttachedFuncSymbolName(typeDef.symbol.name.value, referencedFuncName)); | ||
BSymbol matchingObjFuncSym = symResolver.lookupSymbol(objEnv, funcName, SymTag.VARIABLE); | ||
|
||
if (matchingObjFuncSym != symTable.notFoundSymbol) { | ||
if (referencedFunctions.contains(referencedFuncName)) { | ||
dlog.error(typeRef.pos, DiagnosticCode.REDECLARED_SYMBOL, referencedFuncName); | ||
return; | ||
} | ||
referencedFunctions.add(referencedFuncName); | ||
|
||
if (Symbols.isFunctionDeclaration(matchingObjFuncSym) && Symbols.isFunctionDeclaration( | ||
referencedFunc.symbol)) { | ||
Optional<BLangFunction> matchingFunc = ((BLangObjectTypeNode) typeDef.typeNode) | ||
|
@@ -1791,7 +1817,10 @@ private boolean hasSameFunctionSignature(BInvokableSymbol attachedFuncSym, BInvo | |
|
||
List<BVarSymbol> params = referencedFuncSym.params; | ||
for (int i = 0; i < params.size(); i++) { | ||
if (!params.get(i).name.value.equals(attachedFuncSym.params.get(i).name.value)) { | ||
BVarSymbol referencedFuncParam = params.get(i); | ||
BVarSymbol attachedFuncParam = attachedFuncSym.params.get(i); | ||
if (!referencedFuncParam.name.value.equals(attachedFuncParam.name.value) || | ||
!hasSameVisibilityModifier(referencedFuncParam.flags, attachedFuncParam.flags)) { | ||
return false; | ||
} | ||
} | ||
|
@@ -1822,7 +1851,8 @@ private String getCompleteFunctionSignature(BInvokableSymbol funcSymbol) { | |
signatureBuilder.append(visibilityModifier).append("function ") | ||
.append(funcSymbol.name.value.split("\\.")[1]); | ||
|
||
funcSymbol.params.forEach(param -> paramListBuilder.add(param.type.toString() + " " + param.name.value)); | ||
funcSymbol.params.forEach(param -> paramListBuilder.add( | ||
(Symbols.isPublic(param) ? "public " : "") + param.type.toString() + " " + param.name.value)); | ||
|
||
if (funcSymbol.restParam != null) { | ||
paramListBuilder.add(((BArrayType) funcSymbol.restParam.type).eType.toString() + "... " + | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Btw, This is not a good stream implementation. we should refactor this later.