Skip to content
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

Add return type restrictions: uncategorized #10588

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/class.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Class
# Number > Number # => false
# Number > Object # => false
# ```
def >(other : T.class) forall T
def >(other : T.class) : Bool forall T
# This is so that the method is expanded differently for each type
{% @type %}
other._lt(self)
Expand Down Expand Up @@ -141,7 +141,7 @@ class Class
# Int32.nilable? # => false
# Nil.nilable? # => true
# ```
def nilable?
def nilable? : Bool
self == ::Nil
end

Expand Down
14 changes: 7 additions & 7 deletions src/colorize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ module Colorize
# io << "green and bold if Colorize.enabled"
# end
# ```
def self.with
def self.with : Colorize::Object(String)
"".colorize
end
end

module Colorize::ObjectExtensions
def colorize
def colorize : Colorize::Object
Colorize::Object.new(self)
end

Expand Down Expand Up @@ -278,7 +278,7 @@ struct Colorize::Object(T)
end
{% end %}

def fore(color : Symbol)
def fore(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@fore = ColorANSI::{{name.camelcase.id}}
Expand All @@ -289,11 +289,11 @@ struct Colorize::Object(T)
raise ArgumentError.new "Unknown color: #{color}"
end

def fore(@fore : Color)
def fore(@fore : Color) : self
self
end

def back(color : Symbol)
def back(color : Symbol) : self
{% for name in COLORS %}
if color == :{{name.id}}
@back = ColorANSI::{{name.camelcase.id}}
Expand All @@ -304,11 +304,11 @@ struct Colorize::Object(T)
raise ArgumentError.new "Unknown color: #{color}"
end

def back(@back : Color)
def back(@back : Color) : self
self
end

def mode(mode : Symbol)
def mode(mode : Symbol) : self
{% for name in MODES %}
if mode == :{{name.id}}
@mode |= MODE_{{name.upcase.id}}_FLAG
Expand Down
4 changes: 2 additions & 2 deletions src/comparable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
module Comparable(T)
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a negative number.
def <(other : T)
def <(other : T) : Bool
cmp = self <=> other
cmp ? cmp < 0 : false
end
Expand Down Expand Up @@ -52,7 +52,7 @@ module Comparable(T)

# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a value greater then `0`.
def >(other : T)
def >(other : T) : Bool
cmp = self <=> other
cmp ? cmp > 0 : false
end
Expand Down
8 changes: 4 additions & 4 deletions src/csv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class CSV
# Advanced the cursor to the next row. Must be called once to position
# the cursor in the first row. Returns `true` if a next row was found,
# `false` otherwise.
def next
def next : Bool
return false if @traversed

row = @row ||= [] of String
Expand Down Expand Up @@ -318,15 +318,15 @@ class CSV
end

# :nodoc:
def indices
def indices : Hash(String, Int32)
@indices || raise(Error.new("Headers not requested"))
end

# :nodoc:
getter? strip

# :nodoc:
def headers?
def headers? : Array(String)?
@headers
end

Expand Down Expand Up @@ -405,7 +405,7 @@ class CSV

# Returns the number of columns in this row, regardless of the number
# of headers (if requested).
def size
def size : Int32
@row.size
end

Expand Down
2 changes: 1 addition & 1 deletion src/csv/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class CSV::Lexer
private abstract def current_char

# Returns the next `Token` in this CSV.
def next_token
def next_token : CSV::Token
if @last_empty_column
@last_empty_column = false
@token.kind = Token::Kind::Cell
Expand Down
2 changes: 1 addition & 1 deletion src/ecr/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ECR::Lexer
@column_number = 1
end

def next_token
def next_token : Token
copy_location_info_to_token

case current_char
Expand Down
4 changes: 2 additions & 2 deletions src/ecr/processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ module ECR
DefaultBufferName = "__str__"

# :nodoc:
def process_file(filename, buffer_name = DefaultBufferName)
def process_file(filename, buffer_name = DefaultBufferName) : String
process_string File.read(filename), filename, buffer_name
end

# :nodoc:
def process_string(string, filename, buffer_name = DefaultBufferName)
def process_string(string, filename, buffer_name = DefaultBufferName) : String
lexer = Lexer.new string
token = lexer.next_token

Expand Down
14 changes: 7 additions & 7 deletions src/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct Enum
# Color::Red + 2 # => Color::Blue
# Color::Red + 3 # => Color.new(3)
# ```
def +(other : Int)
def +(other : Int) : self
self.class.new(value + other)
end

Expand All @@ -197,7 +197,7 @@ struct Enum
# Color::Blue - 2 # => Color::Red
# Color::Blue - 3 # => Color.new(-1)
# ```
def -(other : Int)
def -(other : Int) : self
self.class.new(value - other)
end

Expand All @@ -208,7 +208,7 @@ struct Enum
# ```
# (IOMode::Read | IOMode::Async) # => IOMode::Read | IOMode::Async
# ```
def |(other : self)
def |(other : self) : self
self.class.new(value | other.value)
end

Expand All @@ -219,20 +219,20 @@ struct Enum
# ```
# (IOMode::Read | IOMode::Async) & IOMode::Read # => IOMode::Read
# ```
def &(other : self)
def &(other : self) : self
self.class.new(value & other.value)
end

# Returns the enum member that results from applying a logical
# "xor" operation between this enum member's value and *other*.
# This is mostly useful with flag enums.
def ^(other : self)
def ^(other : self) : self
self.class.new(value ^ other.value)
end

# Returns the enum member that results from applying a logical
# "not" operation of this enum member's value.
def ~
def ~ : self
self.class.new(~value)
end

Expand Down Expand Up @@ -276,7 +276,7 @@ struct Enum
# mode.includes?(IOMode::Read) # => true
# mode.includes?(IOMode::Async) # => false
# ```
def includes?(other : self)
def includes?(other : self) : Bool
(value & other.value) != 0
end

Expand Down
2 changes: 1 addition & 1 deletion src/float/printer/cached_powers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module Float::Printer::CachedPowers

Pow10Cache = {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}

def self.largest_pow10(n, n_bits)
def self.largest_pow10(n, n_bits) : {Int32, Int32}
# 1233/4096 is approximately 1/lg(10).
# We increment to skip over the first entry in the powers cache.
guess = ((n_bits + 1) * 1233 >> 12) + 1
Expand Down
10 changes: 5 additions & 5 deletions src/float/printer/diy_fp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Float::Printer::DiyFP
# must be greater than the *other*.
#
# NOTE: This result is not normalized.
def -(other : DiyFP)
def -(other : DiyFP) : self
self.class.new(frac - other.frac, exp)
end

Expand All @@ -76,7 +76,7 @@ struct Float::Printer::DiyFP
# bits.
#
# NOTE: This result is not normalized.
def *(other : DiyFP)
def *(other : DiyFP) : self
a = frac >> 32
b = frac & MASK32
c = other.frac >> 32
Expand All @@ -95,7 +95,7 @@ struct Float::Printer::DiyFP
self.class.new(f, e)
end

def normalize
def normalize : DiyFP
f = frac
e = exp

Expand All @@ -118,13 +118,13 @@ struct Float::Printer::DiyFP
DiyFP.new(f, e)
end

def self.from_f(d : Float64 | Float32)
def self.from_f(d : Float64 | Float32) : self
frac, exp = IEEE.frac_and_exp(d)
new(frac, exp)
end

# Normalize such that the most significant bit of `frac` is set.
def self.from_f_normalized(v : Float64 | Float32)
def self.from_f_normalized(v : Float64 | Float32) : self
pre_normalized = from_f(v)
f = pre_normalized.frac
e = pre_normalized.exp
Expand Down
2 changes: 1 addition & 1 deletion src/float/printer/grisu3.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module Float::Printer::Grisu3
# representable number to the input.
#
# Modifies the generated digits in the buffer to approach (round towards) *w*.
def round_weed(buffer_ptr, length, distance_too_high_w, unsafe_interval, rest, ten_kappa, unit)
def round_weed(buffer_ptr, length, distance_too_high_w, unsafe_interval, rest, ten_kappa, unit) : Bool
buffer = buffer_ptr.to_slice(128)
small_distance = distance_too_high_w - unit
big_distance = distance_too_high_w + unit
Expand Down
24 changes: 12 additions & 12 deletions src/float/printer/ieee.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,35 @@ module Float::Printer::IEEE
DENORMAL_EXPONENT_32 = -EXPONENT_BIAS_32 + 1
SIGN_MASK_32 = 0x80000000_u32

def to_uint(v : Float64)
def to_uint(v : Float64) : UInt64
v.unsafe_as(UInt64)
end

def to_uint(v : Float32)
def to_uint(v : Float32) : UInt32
v.unsafe_as(UInt32)
end

def sign(d64 : UInt64)
def sign(d64 : UInt64) : Int32
(d64 & SIGN_MASK_64) == 0 ? 1 : -1
end

def sign(d32 : UInt32)
def sign(d32 : UInt32) : Int32
(d32 & SIGN_MASK_32) == 0 ? 1 : -1
end

def special?(d64 : UInt64)
def special?(d64 : UInt64) : Bool
(d64 & EXPONENT_MASK_64) == EXPONENT_MASK_64
end

def special?(d32 : UInt32)
def special?(d32 : UInt32) : Bool
(d32 & EXPONENT_MASK_32) == EXPONENT_MASK_32
end

def inf?(d64 : UInt64)
def inf?(d64 : UInt64) : Bool
special?(d64) && (d64 & SIGNIFICAND_MASK_64 == 0)
end

def inf?(d32 : UInt32)
def inf?(d32 : UInt32) : Bool
special?(d32) && (d32 & SIGNIFICAND_MASK_32 == 0)
end

Expand All @@ -96,7 +96,7 @@ module Float::Printer::IEEE
# exponent as *m_plus*.
#
# Precondition: the value encoded by this `Float` must be greater than 0.
def normalized_boundaries(v : Float64)
def normalized_boundaries(v : Float64) : {minus: DiyFP, plus: DiyFP}
w = DiyFP.from_f(v)
m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize

Expand Down Expand Up @@ -124,7 +124,7 @@ module Float::Printer::IEEE
return {minus: m_minus, plus: m_plus}
end

def normalized_boundaries(v : Float32)
def normalized_boundaries(v : Float32) : {minus: DiyFP, plus: DiyFP}
w = DiyFP.from_f(v)
m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize

Expand All @@ -144,7 +144,7 @@ module Float::Printer::IEEE
return {minus: m_minus, plus: m_plus}
end

def frac_and_exp(v : Float64)
def frac_and_exp(v : Float64) : {UInt64, Int32}
d64 = to_uint(v)

if (d64 & EXPONENT_MASK_64) == 0 # denormal float
Expand All @@ -158,7 +158,7 @@ module Float::Printer::IEEE
{frac, exp}
end

def frac_and_exp(v : Float32)
def frac_and_exp(v : Float32) : {UInt64, Int32}
d32 = to_uint(v)

if (d32 & EXPONENT_MASK_32) == 0 # denormal float
Expand Down
2 changes: 1 addition & 1 deletion src/humanize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct Number
end

# :nodoc:
def self.prefix_index(i, group = 3)
def self.prefix_index(i, group = 3) : Int32
((i - (i > 0 ? 1 : 0)) // group) * group
end

Expand Down
2 changes: 1 addition & 1 deletion src/int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ struct Int
# 0b1101.bits_set?(0b0111) # => false
# 0b1101.bits_set?(0b1100) # => true
# ```
def bits_set?(mask)
def bits_set?(mask) : Bool
(self & mask) == mask
end

Expand Down
4 changes: 2 additions & 2 deletions src/nil.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct Nil
end

# Returns `false`.
def same?(other : Reference)
def same?(other : Reference) : Bool
false
end

Expand Down Expand Up @@ -102,7 +102,7 @@ struct Nil
# Raises `NilAssertionError`.
#
# See also: `Object#not_nil!`.
def not_nil!
def not_nil! : NoReturn
raise NilAssertionError.new
end

Expand Down
Loading