Skip to content

Commit

Permalink
Compiler: let protected initialize define a protected new
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Mar 4, 2019
1 parent 8198f53 commit 81f247e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
12 changes: 12 additions & 0 deletions spec/compiler/semantic/visibility_modifiers_spec.cr
Expand Up @@ -400,4 +400,16 @@ describe "Visibility modifiers" do
),
"undefined local variable or method 'foo'"
end

it "defines protected initialize (#7501)" do
assert_error %(
class Foo
protected def initialize
end
end
Foo.new
),
"protected method 'new' called for Foo.class"
end
end
9 changes: 8 additions & 1 deletion src/compiler/crystal/semantic/new.cr
Expand Up @@ -13,6 +13,13 @@ module Crystal
file_modules.each_value do |file_module|
define_default_new(file_module)
end

# Once we are done with the expansions we mark `initialze` methods
# without an explicit visibility as `protected`.
new_expansions.each do |expansion|
original = expansion[:original]
original.visibility = Visibility::Protected if original.visibility.public?
end
end

def define_default_new(type)
Expand Down Expand Up @@ -130,7 +137,7 @@ module Crystal
new_def.splat_index = splat_index
new_def.double_splat = double_splat.clone
new_def.yields = yields
new_def.visibility = Visibility::Private if visibility.private?
new_def.visibility = visibility
new_def.new = true
new_def.doc = doc
new_def.free_vars = free_vars
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/crystal/types.cr
Expand Up @@ -822,10 +822,6 @@ module Crystal
def add_def(a_def)
a_def.owner = self

if a_def.visibility.public? && a_def.name == "initialize"
a_def.visibility = Visibility::Protected
end

item = DefWithMetadata.new(a_def)

defs = (@defs ||= {} of String => Array(DefWithMetadata))
Expand Down

0 comments on commit 81f247e

Please sign in to comment.