-
Notifications
You must be signed in to change notification settings - Fork 5
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
implement field visibility #345
Open
metagn
wants to merge
4
commits into
nim-lang:master
Choose a base branch
from
metagn:object-field-visibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−16
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -1349,7 +1349,15 @@ proc semCall(c: var SemContext; it: var Item; source: TransformedCallSource = Re | |
else: | ||
resolveOverloads c, it, cs | ||
|
||
proc findObjField(t: Cursor; name: StrId; level = 0): ObjField = | ||
proc genericRootSym(td: TypeDecl): SymId = | ||
result = td.name.symId | ||
if td.typevars.typeKind == InvokeT: | ||
var root = td.typevars | ||
inc root | ||
assert root.kind == Symbol | ||
result = root.symId | ||
|
||
proc findObjFieldAux(t: Cursor; name: StrId; level = 0): ObjField = | ||
assert t == "object" | ||
var n = t | ||
inc n # skip `(object` token | ||
|
@@ -1360,9 +1368,10 @@ proc findObjField(t: Cursor; name: StrId; level = 0): ObjField = | |
if n.kind == SymbolDef and sameIdent(n.symId, name): | ||
let symId = n.symId | ||
inc n # skip name | ||
let exported = n.kind != DotToken | ||
skip n # export marker | ||
skip n # pragmas | ||
return ObjField(sym: symId, level: level, typ: n) | ||
return ObjField(sym: symId, level: level, typ: n, exported: exported, rootOwner: SymId(0)) | ||
skip n # skip name | ||
skip n # export marker | ||
skip n # pragmas | ||
|
@@ -1377,11 +1386,44 @@ proc findObjField(t: Cursor; name: StrId; level = 0): ObjField = | |
if baseType.typeKind == InvokeT: | ||
inc baseType # get to root symbol | ||
if baseType.kind == Symbol: | ||
result = findObjField(objtypeImpl(baseType.symId), name, level+1) | ||
let decl = getTypeSection(baseType.symId) | ||
var objType = decl.body | ||
# emulate objtypeImpl | ||
if objType.typeKind in {RefT, PtrT}: | ||
inc objType | ||
result = findObjFieldAux(objType, name, level+1) | ||
if result.level == level+1: | ||
result.rootOwner = genericRootSym(decl) | ||
else: | ||
# maybe error | ||
result = ObjField(level: -1) | ||
|
||
proc findObjFieldConsiderVis(c: var SemContext; decl: TypeDecl; name: StrId; info: PackedLineInfo): ObjField = | ||
var impl = decl.body | ||
# emulate objtypeImpl | ||
if impl.typeKind in {RefT, PtrT}: | ||
inc impl | ||
result = findObjFieldAux(impl, name) | ||
if result.level == 0: | ||
result.rootOwner = genericRootSym(decl) | ||
if result.level >= 0: | ||
# check visibility | ||
var visible = false | ||
if result.exported: | ||
visible = true | ||
else: | ||
let owner = result.rootOwner | ||
if owner == SymId(0): | ||
visible = true | ||
else: | ||
let ownerModule = extractModule(pool.syms[owner]) | ||
# safe to get this from line info? | ||
let currentModule = moduleSuffix(getFile(info), c.g.config.paths) | ||
visible = ownerModule == "" or currentModule == "" or ownerModule == currentModule | ||
if not visible: | ||
# treat as undeclared | ||
result = ObjField(level: -1) | ||
|
||
proc findModuleSymbol(n: Cursor): SymId = | ||
result = SymId(0) | ||
if n.kind == Symbol: | ||
|
@@ -1432,9 +1474,13 @@ proc tryBuiltinDot(c: var SemContext; it: var Item; lhs: Item; fieldName: StrId; | |
if root.typeKind == InvokeT: | ||
inc root | ||
if root.kind == Symbol: | ||
let objType = objtypeImpl(root.symId) | ||
let decl = getTypeSection(root.symId) | ||
var objType = decl.body | ||
# emulate objtypeImpl | ||
if objType.typeKind in {RefT, PtrT}: | ||
inc objType | ||
if objType.typeKind == ObjectT: | ||
let field = findObjField(objType, fieldName) | ||
let field = findObjFieldConsiderVis(c, decl, fieldName, info) | ||
if field.level >= 0: | ||
c.dest.add symToken(field.sym, info) | ||
c.dest.add intToken(pool.integers.getOrIncl(field.level), info) | ||
|
@@ -1767,7 +1813,10 @@ proc semObjectType(c: var SemContext; n: var Cursor) = | |
takeToken c, n | ||
else: | ||
# object fields: | ||
let oldScopeKind = c.currentScope.kind | ||
withNewScope c: | ||
# copy toplevel scope status for exported fields | ||
c.currentScope.kind = oldScopeKind | ||
while n.substructureKind == FldS: | ||
semLocal(c, n, FldY) | ||
wantParRi c, n | ||
|
@@ -3273,8 +3322,11 @@ proc semTypeSection(c: var SemContext; n: var Cursor) = | |
takeToken c, n | ||
isGeneric = false | ||
else: | ||
let oldScopeKind = c.currentScope.kind | ||
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? |
||
openScope c | ||
semGenericParams c, n | ||
# copy toplevel scope status for exported fields | ||
c.currentScope.kind = oldScopeKind | ||
isGeneric = true | ||
|
||
semTypePragmas c, n, beforeExportMarker | ||
|
@@ -3606,13 +3658,18 @@ proc semObjConstr(c: var SemContext, it: var Item) = | |
inc it.n | ||
it.typ = semLocalType(c, it.n) | ||
c.dest.shrink exprStart | ||
var decl = default(TypeDecl) | ||
var objType = it.typ | ||
if objType.typeKind in {RefT, PtrT}: | ||
inc objType | ||
if objType.typeKind == InvokeT: | ||
inc objType | ||
if objType.kind == Symbol: | ||
objType = objtypeImpl(objType.symId) | ||
decl = getTypeSection(objType.symId) | ||
objType = decl.body | ||
# emulate objtypeImpl | ||
if objType.typeKind in {RefT, PtrT}: | ||
inc objType | ||
if objType.typeKind != ObjectT: | ||
c.buildErr info, "expected object type for object constructor" | ||
return | ||
|
@@ -3641,9 +3698,9 @@ proc semObjConstr(c: var SemContext, it: var Item) = | |
# level is not known but not used either, set it to 0: | ||
field = ObjField(sym: sym, typ: asLocal(res.decl).typ, level: 0) | ||
else: | ||
field = findObjField(objType, fieldName) | ||
field = findObjFieldConsiderVis(c, decl, fieldName, info) | ||
else: | ||
field = findObjField(objType, fieldName) | ||
field = findObjFieldConsiderVis(c, decl, fieldName, info) | ||
if field.level >= 0: | ||
if field.sym in setFieldPositions: | ||
c.buildErr fieldInfo, "field already set: " & pool.strings[fieldName] | ||
|
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type Foo* = object | ||
public*: int | ||
private: int | ||
|
||
type Generic*[T] = object | ||
public*: T | ||
private: T | ||
|
||
proc getPrivate*(x: Foo): int = x.private | ||
proc getPrivate*[T](x: Generic[T]): T = x.private | ||
|
||
template getPrivateTempl*(x: Foo): int = x.private | ||
template getPrivateTempl*[T](x: Generic[T]): T = T(x.private) |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tests/nimony/modules/tfieldviserr.nim(3, 15) Error: undeclared field: private | ||
tests/nimony/modules/tfieldviserr.nim(4, 12) Error: undeclared identifier | ||
tests/nimony/modules/tfieldviserr.nim(6, 28) Error: undeclared field: private | ||
tests/nimony/modules/tfieldviserr.nim(7, 16) Error: undeclared identifier |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import deps/mfieldvis | ||
|
||
var foo = Foo(private: 123) | ||
discard foo.private | ||
|
||
var generic = Generic[int](private: 123) | ||
discard generic.private | ||
|
||
when false: # compiles and should compile but cannot test yet | ||
var foo = Foo(public: 123) | ||
discard getPrivate(foo) | ||
discard getPrivateTempl(foo) | ||
|
||
var generic = Generic[int](public: 123) | ||
discard getPrivate(foo) | ||
discard getPrivateTempl(foo) | ||
|
||
template resem() = | ||
foo = Foo(public: 123) | ||
generic = Generic[int](public: 123) | ||
resem() |
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.
Wait ... what? Why?