Skip to content

Commit

Permalink
Fix constructor method resolution (#293)
Browse files Browse the repository at this point in the history
Restrict `new`, `self.init()` and `super.init()` to calling immediate members of the class (`new` and `self.init()`) or the class's immediate superclass (`super.init`)
  • Loading branch information
degory authored Aug 21, 2020
1 parent 3937159 commit af23104
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
9 changes: 7 additions & 2 deletions lib/ghul.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ namespace System is
si

class StringBuffer: String is
init();
init(initial_capacity: int);
init(s: String);
append(value: Object) -> StringBuffer;
Expand All @@ -615,26 +616,29 @@ namespace System is
si

class Exception is
init();
init(message: String);
si

class NullPointerException is
init();
init(message: String);
si

class NotImplementedException is
init();
init(message: String);
si

class BoundsException is
init();
init(message: String);
si

class Arguments is
ProgramArguments: String[];
ProgramEnvironment:String[];
si

si
si

@IL.stub()
Expand Down Expand Up @@ -749,6 +753,7 @@ namespace Generic is
si

class Map[K,V]: Dict[K,V] is
init();
init(size: int);

clear();
Expand Down
1 change: 0 additions & 1 deletion src/semantic/graph/value/value.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ namespace Semantic.Graph.Value is
context.println(call);
si


toString() -> String =>
"closure-call:[" + type + "](" + from + "," + arguments + ")";
si
Expand Down
15 changes: 11 additions & 4 deletions src/semantic/symbol/symbol.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ namespace Semantic.Symbol is
si

declare_property(location: LOCATION, name: String, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener) -> Scope is
var result = new Symbol.GLOBAL_PROPERTY(location, self, name);
var result = new Symbol.GLOBAL_PROPERTY(location, self, name, is_assignable);

declare(location, result, symbol_definition_listener);

Expand Down Expand Up @@ -2076,6 +2076,11 @@ namespace Semantic.Symbol is
merge(group: FUNCTION_GROUP) is
group.resolve_overrides();

if group.name =~ "init" then
IO.Std.err.println("owner: " + self);
IO.Std.err.println("merging init function group: " + group);
fi

for overridee in group.functions do
var overrider = find_override(overridee);

Expand Down Expand Up @@ -2132,8 +2137,10 @@ namespace Semantic.Symbol is
"non-virtual method cannot override " + overridee + " (defined at " + overridee.location + ")"
);
fi
else
elif overridee.name !~ "init" then
add(overridee);
else
IO.Std.err.println("do not pull init from " + overridee);
fi
od
si
Expand Down Expand Up @@ -2395,8 +2402,8 @@ namespace Semantic.Symbol is
class GLOBAL_PROPERTY: Property is
description: String => qualified_name + ": " + type + " // global property";

init(location: LOCATION, owner: Scope, name: String) is
super.init(location, owner, name);
init(location: LOCATION, owner: Scope, name: String, is_assignable: bool) is
super.init(location, owner, name, is_assignable);
si

load(location: LOCATION, from: Graph.Value.BASE, loader: SYMBOL_LOADER) -> Graph.Value.BASE is
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/process/compile_expressions.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ namespace Syntax.Process is

let named_type = cast Semantic.Type.NAMED(type);

let symbol = named_type.scope.find_member("init");
let symbol = named_type.scope.find_direct("init");

let function_group = cast Semantic.Symbol.FUNCTION_GROUP(symbol);

Expand Down
2 changes: 1 addition & 1 deletion src/syntax/process/signature_help.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ namespace Syntax.Process is

let named_type = cast Semantic.Type.NAMED(type);

let symbol = named_type.scope.find_member("init");
let symbol = named_type.scope.find_direct("init");

let function_group = cast Semantic.Symbol.FUNCTION_GROUP(symbol);

Expand Down
6 changes: 3 additions & 3 deletions src/system/gsmap.ghul
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ namespace Collection is
color: NodeColor public;

init(key: K, value: V, nodeNodeColor: NodeColor, left: TreeNode[K,V], right: TreeNode[K,V]) is
super.init();
super.init(key, value);

self.key = key;
self.value = value;
// self.key = key;
// self.value = value;
self.color = nodeNodeColor;
self.left = left;
self.right = right;
Expand Down

0 comments on commit af23104

Please sign in to comment.