From 0d48469b41137b40f37e64ac87e0546f96e551cd Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:16:23 -0300 Subject: [PATCH 01/14] Compiler: refactor, add Program#report_warning(node, message) --- src/compiler/crystal/semantic/abstract_def_checker.cr | 5 +---- src/compiler/crystal/semantic/warnings.cr | 10 ++++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/compiler/crystal/semantic/warnings.cr diff --git a/src/compiler/crystal/semantic/abstract_def_checker.cr b/src/compiler/crystal/semantic/abstract_def_checker.cr index 46aa702d805e..418cf388c609 100644 --- a/src/compiler/crystal/semantic/abstract_def_checker.cr +++ b/src/compiler/crystal/semantic/abstract_def_checker.cr @@ -230,10 +230,7 @@ class Crystal::AbstractDefChecker end private def report_warning(node, message) - return unless @program.warnings.all? - return if @program.ignore_warning_due_to_location?(node.location) - - @program.warning_failures << node.warning(message) + @program.report_warning(node, message) end class ReplacePathWithTypeVar < Visitor diff --git a/src/compiler/crystal/semantic/warnings.cr b/src/compiler/crystal/semantic/warnings.cr new file mode 100644 index 000000000000..d74f6e495870 --- /dev/null +++ b/src/compiler/crystal/semantic/warnings.cr @@ -0,0 +1,10 @@ +module Crystal + class Program + def report_warning(node : ASTNode, message : String) + return unless self.warnings.all? + return if self.ignore_warning_due_to_location?(node.location) + + self.warning_failures << node.warning(message) + end + end +end From 764f8ef9cceabb754e536eed95565c602ad5f2a6 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:18:19 -0300 Subject: [PATCH 02/14] Compiler: inline `relative_filename` overload --- src/compiler/crystal/util.cr | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/compiler/crystal/util.cr b/src/compiler/crystal/util.cr index 9e5ef4a69bfa..10caed8bfbca 100644 --- a/src/compiler/crystal/util.cr +++ b/src/compiler/crystal/util.cr @@ -1,5 +1,7 @@ module Crystal - def self.relative_filename(filename : String) : String + def self.relative_filename(filename) + return filename unless filename.is_a?(String) + if base_file = filename.lchop? Dir.current if file_prefix = base_file.lchop? '/' return file_prefix @@ -9,10 +11,6 @@ module Crystal filename end - def self.relative_filename(filename) - filename - end - def self.error(msg, color, exit_code = 1, stderr = STDERR, leading_error = true) stderr.print "Error: ".colorize.toggle(color).red.bold if leading_error stderr.puts msg.colorize.toggle(color).bright From 34d2e7288fc40c1afb47f942c05f4aaacf15134e Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:29:18 -0300 Subject: [PATCH 03/14] Remove useless abstract defs in IO::ByteFormat --- src/io/byte_format.cr | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/io/byte_format.cr b/src/io/byte_format.cr index ff28be93f6e8..6f6b6a1082cd 100644 --- a/src/io/byte_format.cr +++ b/src/io/byte_format.cr @@ -43,8 +43,6 @@ module IO::ByteFormat abstract def encode(int : UInt64, io : IO) abstract def encode(int : Int128, io : IO) abstract def encode(int : UInt128, io : IO) - abstract def encode(int : Float32, io : IO) - abstract def encode(int : Float64, io : IO) abstract def encode(int : Int8, bytes : Bytes) abstract def encode(int : UInt8, bytes : Bytes) @@ -56,8 +54,6 @@ module IO::ByteFormat abstract def encode(int : UInt64, bytes : Bytes) abstract def encode(int : Int128, bytes : Bytes) abstract def encode(int : UInt128, bytes : Bytes) - abstract def encode(int : Float32, bytes : Bytes) - abstract def encode(int : Float64, bytes : Bytes) abstract def decode(int : Int8.class, io : IO) abstract def decode(int : UInt8.class, io : IO) @@ -69,8 +65,6 @@ module IO::ByteFormat abstract def decode(int : UInt64.class, io : IO) abstract def decode(int : Int128.class, io : IO) abstract def decode(int : UInt128.class, io : IO) - abstract def decode(int : Float32.class, io : IO) - abstract def decode(int : Float64.class, io : IO) abstract def decode(int : Int8.class, bytes : Bytes) abstract def decode(int : UInt8.class, bytes : Bytes) @@ -82,8 +76,6 @@ module IO::ByteFormat abstract def decode(int : UInt64.class, bytes : Bytes) abstract def decode(int : Int128.class, bytes : Bytes) abstract def decode(int : UInt128.class, bytes : Bytes) - abstract def decode(int : Float32.class, bytes : Bytes) - abstract def decode(int : Float64.class, bytes : Bytes) def encode(float : Float32, io : IO) encode(float.unsafe_as(Int32), io) From 2afe24726c21908f1ff091818dc9e6780efed160 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:29:59 -0300 Subject: [PATCH 04/14] Remove duplicated XML::Node initializer --- src/xml/node.cr | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/xml/node.cr b/src/xml/node.cr index 828ff4736804..6a4bca8d79b8 100644 --- a/src/xml/node.cr +++ b/src/xml/node.cr @@ -11,11 +11,6 @@ struct XML::Node initialize(node.as(LibXML::Node*)) end - # ditto - def initialize(node : LibXML::Doc*) - initialize(node.as(LibXML::Node*)) - end - # ditto def initialize(@node : LibXML::Node*) end From 2c1b2f160f5e4125474ea4d3bd391f5059705b15 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:31:12 -0300 Subject: [PATCH 05/14] Remove duplicated `UUID#bytes` method --- src/uuid.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uuid.cr b/src/uuid.cr index f9596b790a09..7e7c36e70993 100644 --- a/src/uuid.cr +++ b/src/uuid.cr @@ -30,7 +30,7 @@ struct UUID V5 = 5 end - protected getter bytes : StaticArray(UInt8, 16) + @bytes : StaticArray(UInt8, 16) # Generates UUID from *bytes*, applying *version* and *variant* to the UUID if # present. From a84437faca448f75a9280443655a6e03e2fa2d52 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:32:52 -0300 Subject: [PATCH 06/14] Remove duplicated InitProject#views method --- src/compiler/crystal/tools/init.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/crystal/tools/init.cr b/src/compiler/crystal/tools/init.cr index 7a191055ea5f..a559ea129089 100644 --- a/src/compiler/crystal/tools/init.cr +++ b/src/compiler/crystal/tools/init.cr @@ -206,7 +206,7 @@ module Crystal class InitProject getter config : Config - getter views : Array(View)? + @views : Array(View)? def initialize(@config : Config) end From 9f4645b0bf6798a6b753dfc80e29e920de349c28 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:34:45 -0300 Subject: [PATCH 07/14] Remove duplicated Codegen#llvm_typer method --- src/compiler/crystal/codegen/codegen.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/compiler/crystal/codegen/codegen.cr b/src/compiler/crystal/codegen/codegen.cr index c40b58ab1040..df6afa474132 100644 --- a/src/compiler/crystal/codegen/codegen.cr +++ b/src/compiler/crystal/codegen/codegen.cr @@ -257,7 +257,6 @@ module Crystal emit_vars_debug_info(@program.vars) if @debug.variables? end - getter llvm_typer getter llvm_context def new_builder(llvm_context) From 3b378df708a8e8661c1159c4fcd1b11517d16437 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:37:21 -0300 Subject: [PATCH 08/14] Remove duplicated `filter_by_responds_to` method --- src/compiler/crystal/types.cr | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/compiler/crystal/types.cr b/src/compiler/crystal/types.cr index b41522c73bd7..dc72c49643a7 100644 --- a/src/compiler/crystal/types.cr +++ b/src/compiler/crystal/types.cr @@ -2099,10 +2099,6 @@ module Crystal end end - def filter_by_responds_to(name) - including_types.try &.filter_by_responds_to(name) - end - def filter_by_responds_to(name) @generic_type.filter_by_responds_to(name) ? self : nil end From af1f6c44481ab413a01cbe22d74594f5315320d6 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:39:40 -0300 Subject: [PATCH 09/14] Remove duplicated `struct?` method --- src/compiler/crystal/types.cr | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/compiler/crystal/types.cr b/src/compiler/crystal/types.cr index dc72c49643a7..f74d42d9c1d1 100644 --- a/src/compiler/crystal/types.cr +++ b/src/compiler/crystal/types.cr @@ -1192,10 +1192,6 @@ module Crystal superclass.try &.add_subclass(self) end - def struct? - @struct - end - def has_attribute?(name) return true if packed? && name == "Packed" false From 54e2608b6f889accb65ce8c68db0d522c909df7b Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:42:49 -0300 Subject: [PATCH 10/14] Remove duplicated `name_size` method --- src/compiler/crystal/syntax/ast.cr | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/crystal/syntax/ast.cr b/src/compiler/crystal/syntax/ast.cr index 7b5b8be6ea42..6baed31bd27d 100644 --- a/src/compiler/crystal/syntax/ast.cr +++ b/src/compiler/crystal/syntax/ast.cr @@ -539,7 +539,7 @@ module Crystal property block_arg : ASTNode? property named_args : Array(NamedArgument)? property name_location : Location? - property name_size = -1 + @name_size = -1 property doc : String? property visibility = Visibility::Public property? global : Bool @@ -575,6 +575,8 @@ module Crystal @name_size end + setter name_size + def accept_children(visitor) @obj.try &.accept visitor @args.each &.accept visitor From 8755a27b6d5438af72fb3f06a0c2b1bb11deacdc Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:44:22 -0300 Subject: [PATCH 11/14] Remove duplicated LLVM::Builder si2fp and ui2f methods --- src/llvm/builder.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llvm/builder.cr b/src/llvm/builder.cr index 4a1a99bc2dcf..7180212fbbff 100644 --- a/src/llvm/builder.cr +++ b/src/llvm/builder.cr @@ -127,7 +127,7 @@ class LLVM::Builder Value.new LibLLVM.build_extract_value(self, value, index, name) end - {% for name in %w(bit_cast si2fp ui2fp zext sext trunc fpext fptrunc fp2si fp2ui si2fp ui2fp int2ptr ptr2int) %} + {% for name in %w(bit_cast zext sext trunc fpext fptrunc fp2si fp2ui si2fp ui2fp int2ptr ptr2int) %} def {{name.id}}(value, type, name = "") # check_type({{name}}, type) # check_value(value) From 2047d947872c892d15469966d113bea182018977 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:44:56 -0300 Subject: [PATCH 12/14] Remove duplicated `URI.parse` method --- src/uri.cr | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/uri.cr b/src/uri.cr index 4cdca7378f9e..e60a807ef36b 100644 --- a/src/uri.cr +++ b/src/uri.cr @@ -502,19 +502,6 @@ class URI end end - # Parses `raw_url` into an URI. The `raw_url` may be relative or absolute. - # - # ``` - # require "uri" - # - # uri = URI.parse("http://crystal-lang.org") # => # - # uri.scheme # => "http" - # uri.host # => "crystal-lang.org" - # ``` - def self.parse(raw_url : String) : URI - URI::Parser.new(raw_url).run.uri - end - # [RFC 3986 6.2.2.3](https://tools.ietf.org/html/rfc3986#section-5.2.4) private def remove_dot_segments(path : String) result = [] of String From d323bd1f23d62dc0bd5e5ecd227dd34b9b5fc579 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:47:39 -0300 Subject: [PATCH 13/14] Remove duplicated `String::Builder#buffer` method --- src/string/builder.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string/builder.cr b/src/string/builder.cr index c7dc2adf7867..bd6f94b8f15c 100644 --- a/src/string/builder.cr +++ b/src/string/builder.cr @@ -6,7 +6,7 @@ require "io" class String::Builder < IO getter bytesize : Int32 getter capacity : Int32 - getter buffer : Pointer(UInt8) + @buffer : Pointer(UInt8) def initialize(capacity : Int = 64) String.check_capacity_in_bounds(capacity) From 3e8d7ddc0dbd3d41effef625a6583f4f68cf6b47 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Wed, 2 Oct 2019 13:48:07 -0300 Subject: [PATCH 14/14] Add type restrictions to MIME::Multipart.build to enable overloads --- src/mime/multipart.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mime/multipart.cr b/src/mime/multipart.cr index d5ba88c32388..f5126fe057f7 100644 --- a/src/mime/multipart.cr +++ b/src/mime/multipart.cr @@ -83,7 +83,7 @@ module MIME::Multipart # Yields a `Multipart::Builder` to the given block, writing to *io* and # using *boundary*. `#finish` is automatically called on the builder. - def self.build(io, boundary = Multipart.generate_boundary) + def self.build(io : IO, boundary : String = Multipart.generate_boundary) builder = Builder.new(io, boundary) yield builder builder.finish @@ -91,7 +91,7 @@ module MIME::Multipart # Yields a `Multipart::Builder` to the given block, returning the generated # message as a `String`. - def self.build(boundary = Multipart.generate_boundary) + def self.build(boundary : String = Multipart.generate_boundary) String.build do |io| build(io, boundary) { |g| yield g } end @@ -104,7 +104,7 @@ module MIME::Multipart # # MIME::Multipart.generate_boundary # => "---------------------------dQu6bXHYb4m5zrRC3xPTGwV" # ``` - def self.generate_boundary + def self.generate_boundary : String "--------------------------#{Random::Secure.urlsafe_base64(18)}" end