Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ end
task :validate => :compile do
require 'yaml'

sh "#{ruby} #{rbs} validate --exit-error-on-syntax-error"
sh "#{ruby} #{rbs} validate"

libs = FileList["stdlib/*"].map {|path| File.basename(path).to_s }

Expand Down Expand Up @@ -219,7 +219,7 @@ task :validate => :compile do
args << "prism"
end

sh "#{ruby} #{rbs} #{args.join(' ')} validate --exit-error-on-syntax-error"
sh "#{ruby} #{rbs} #{args.join(' ')} validate"
end
end

Expand Down
13 changes: 8 additions & 5 deletions ext/rbs_extension/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct parse_type_arg {
VALUE require_eof;
VALUE void_allowed;
VALUE self_allowed;
VALUE classish_allowed;
};

struct parse_method_type_arg {
Expand Down Expand Up @@ -118,9 +119,10 @@ static VALUE parse_type_try(VALUE a) {

bool void_allowed = RTEST(arg->void_allowed);
bool self_allowed = RTEST(arg->self_allowed);
bool classish_allowed = RTEST(arg->classish_allowed);

rbs_node_t *type;
rbs_parse_type(parser, &type, void_allowed, self_allowed);
rbs_parse_type(parser, &type, void_allowed, self_allowed, classish_allowed);

raise_error_if_any(parser, arg->buffer);

Expand Down Expand Up @@ -176,7 +178,7 @@ static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int e
);
}

static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE void_allowed, VALUE self_allowed) {
static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof, VALUE void_allowed, VALUE self_allowed, VALUE classish_allowed) {
VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
StringValue(string);
rb_encoding *encoding = rb_enc_get(string);
Expand All @@ -189,7 +191,8 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL
.parser = parser,
.require_eof = require_eof,
.void_allowed = void_allowed,
.self_allowed = self_allowed
.self_allowed = self_allowed,
.classish_allowed = classish_allowed
};

VALUE result = rb_ensure(parse_type_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser);
Expand All @@ -208,7 +211,7 @@ static VALUE parse_method_type_try(VALUE a) {
}

rbs_method_type_t *method_type = NULL;
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof));
rbs_parse_method_type(parser, &method_type, RB_TEST(arg->require_eof), true);

raise_error_if_any(parser, arg->buffer);

Expand Down Expand Up @@ -449,7 +452,7 @@ void rbs__init_parser(void) {
EMPTY_HASH = rb_obj_freeze(rb_hash_new());
rb_gc_register_mark_object(EMPTY_HASH);

rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 7);
rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 8);
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3);
rb_define_singleton_method(RBS_Parser, "_parse_type_params", rbsparser_parse_type_params, 4);
Expand Down
4 changes: 2 additions & 2 deletions include/rbs/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line

void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);

bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed);
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof);
bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed, bool classish_allowed);
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof, bool classish_allowed);
bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);

bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);
Expand Down
65 changes: 5 additions & 60 deletions lib/rbs/cli/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ module RBS
class CLI
class Validate
class Errors
def initialize(limit:, exit_error:)
def initialize(limit:)
@limit = limit
@exit_error = exit_error
@errors = []
@has_syntax_error = false
end

def add(error)
if error.instance_of?(WillSyntaxError)
RBS.logger.warn(build_message(error))
@has_syntax_error = true
else
@errors << error
end
@errors << error
finish if @limit == 1
end

Expand All @@ -30,13 +23,7 @@ def try(&block)
end

def finish
if @errors.empty?
if @exit_error && @has_syntax_error
throw @tag, 1
else
# success
end
else
unless @errors.empty?
@errors.each do |error|
RBS.logger.error(build_message(error))
end
Expand All @@ -63,7 +50,6 @@ def initialize(args:, options:)
@env = Environment.from_loader(loader).resolve_type_names
@builder = DefinitionBuilder.new(env: @env)
@validator = Validator.new(env: @env)
exit_error = false
limit = nil #: Integer?
OptionParser.new do |opts|
opts.banner = <<EOU
Expand All @@ -80,14 +66,14 @@ def initialize(args:, options:)
RBS.print_warning { "`--silent` option is deprecated because it's silent by default. You can use --log-level option of rbs command to display more information." }
end
opts.on("--[no-]exit-error-on-syntax-error", "exit(1) if syntax error is detected") {|bool|
exit_error = bool
RBS.print_warning { "`--exit-error-on-syntax-error` option is deprecated because it's validated during parsing.." }
}
opts.on("--fail-fast", "Exit immediately as soon as a validation error is found.") do |arg|
limit = 1
end
end.parse!(args)

@errors = Errors.new(limit: limit, exit_error: exit_error)
@errors = Errors.new(limit: limit)
end

def run
Expand Down Expand Up @@ -122,7 +108,6 @@ def validate_class_module_definition
entry.each_decl do |decl|
if super_class = decl.super_class
super_class.args.each do |arg|
no_classish_type_validator(arg)
@validator.validate_type(arg, context: nil)
end
end
Expand All @@ -131,7 +116,6 @@ def validate_class_module_definition
entry.each_decl do |decl|
decl.self_types.each do |self_type|
self_type.args.each do |arg|
no_classish_type_validator(arg)
@validator.validate_type(arg, context: nil)
end

Expand Down Expand Up @@ -159,17 +143,14 @@ def validate_class_module_definition

d.type_params.each do |param|
if ub = param.upper_bound_type
no_classish_type_validator(ub)
@validator.validate_type(ub, context: nil)
end

if lb = param.lower_bound_type
no_classish_type_validator(lb)
@validator.validate_type(lb, context: nil)
end

if dt = param.default_type
no_classish_type_validator(dt)
@validator.validate_type(dt, context: nil)
end
end
Expand Down Expand Up @@ -230,17 +211,14 @@ def validate_interface

decl.decl.type_params.each do |param|
if ub = param.upper_bound_type
no_classish_type_validator(ub)
@validator.validate_type(ub, context: nil)
end

if lb = param.lower_bound_type
no_classish_type_validator(lb)
@validator.validate_type(lb, context: nil)
end

if dt = param.default_type
no_classish_type_validator(dt)
@validator.validate_type(dt, context: nil)
end
end
Expand All @@ -251,9 +229,6 @@ def validate_interface
case member
when AST::Members::MethodDefinition
@validator.validate_method_definition(member, type_name: name)
member.overloads.each do |ov|
no_classish_type_validator(ov.method_type)
end
end
end
rescue BaseError => error
Expand All @@ -266,7 +241,6 @@ def validate_constant
RBS.logger.info "Validating constant: `#{name}`..."
@validator.validate_type const.decl.type, context: const.context
@builder.ensure_namespace!(name.namespace, location: const.decl.location)
no_classish_type_validator(const.decl.type)
rescue BaseError => error
@errors.add(error)
end
Expand All @@ -276,7 +250,6 @@ def validate_global
@env.global_decls.each do |name, global|
RBS.logger.info "Validating global: `#{name}`..."
@validator.validate_type global.decl.type, context: nil
no_classish_type_validator(global.decl.type)
rescue BaseError => error
@errors.add(error)
end
Expand All @@ -299,51 +272,23 @@ def validate_type_alias

decl.decl.type_params.each do |param|
if ub = param.upper_bound_type
no_classish_type_validator(ub)
@validator.validate_type(ub, context: nil)
end

if lb = param.lower_bound_type
no_classish_type_validator(lb)
@validator.validate_type(lb, context: nil)
end

if dt = param.default_type
no_classish_type_validator(dt)
@validator.validate_type(dt, context: nil)
end
end

TypeParamDefaultReferenceError.check!(decl.decl.type_params)

no_classish_type_validator(decl.decl.type)
rescue BaseError => error
@errors.add(error)
end
end

private

def no_self_type_validator(type)
if type.has_self_type?
@errors.add WillSyntaxError.new("`self` type is not allowed in this context", location: type.location)
end
end

def no_classish_type_validator(type)
if type.has_classish_type?
@errors.add WillSyntaxError.new("`instance` or `class` type is not allowed in this context", location: type.location)
end
end

def void_type_context_validator(type, allowed_here = false)
if allowed_here
return if type.is_a?(Types::Bases::Void)
end
if type.with_nonreturn_void? # steep:ignore DeprecatedReference
@errors.add WillSyntaxError.new("`void` type is only allowed in return type or generics parameter", location: type.location)
end
end
end
end
end
11 changes: 0 additions & 11 deletions lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,6 @@ def location
end
end

class WillSyntaxError < DefinitionError
include DetailedMessageable

attr_reader :location

def initialize(message, location:)
super "#{Location.to_string(location)}: #{message}"
@location = location
end
end

class TypeParamDefaultReferenceError < DefinitionError
include DetailedMessageable

Expand Down
4 changes: 2 additions & 2 deletions lib/rbs/parser_aux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

module RBS
class Parser
def self.parse_type(source, range: 0..., variables: [], require_eof: false, void_allowed: true, self_allowed: true)
def self.parse_type(source, range: 0..., variables: [], require_eof: false, void_allowed: true, self_allowed: true, classish_allowed: true)
buf = buffer(source)
_parse_type(buf, range.begin || 0, range.end || buf.last_position, variables, require_eof, void_allowed, self_allowed)
_parse_type(buf, range.begin || 0, range.end || buf.last_position, variables, require_eof, void_allowed, self_allowed, classish_allowed)
end

def self.parse_method_type(source, range: 0..., variables: [], require_eof: false)
Expand Down
7 changes: 1 addition & 6 deletions sig/cli/validate.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module RBS
class Validate
class Errors
@limit: Integer?
@exit_error: boolish
@has_syntax_error: bool
@errors: Array[BaseError]

# The tag that will be thrown in #finish method
@tag: top

def initialize: (limit: Integer?, exit_error: boolish) -> void
def initialize: (limit: Integer?) -> void

def add: (BaseError) -> void

Expand Down Expand Up @@ -44,9 +42,6 @@ module RBS
def validate_constant: () -> void
def validate_global: () -> void
def validate_type_alias: () -> void
def no_classish_type_validator: (::RBS::Types::t | ::RBS::MethodType type) -> void
def no_self_type_validator: (::RBS::Types::t | ::RBS::MethodType type) -> void
%a{deprecated} def void_type_context_validator: (::RBS::Types::t | ::RBS::MethodType type, ?bool allowed_here) -> void
end
end
end
8 changes: 0 additions & 8 deletions sig/errors.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,6 @@ module RBS
def location: () -> AST::Declarations::AliasDecl::loc?
end

class WillSyntaxError < BaseError
include RBS::DetailedMessageable

def initialize: (String message, location: Location[untyped, untyped]?) -> void

attr_reader location: Location[untyped, untyped]?
end

class TypeParamDefaultReferenceError < BaseError
include DetailedMessageable

Expand Down
4 changes: 2 additions & 2 deletions sig/parser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module RBS
# RBS::Parser.parse_type("self", self_allowed: false) # => Raises an syntax error
# ```
#
def self.parse_type: (Buffer | String, ?range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool, ?void_allowed: bool, ?self_allowed: bool) -> Types::t?
def self.parse_type: (Buffer | String, ?range: Range[Integer?], ?variables: Array[Symbol], ?require_eof: bool, ?void_allowed: bool, ?self_allowed: bool, ?classish_allowed: bool) -> Types::t?

# Parse whole RBS file and return an array of declarations
#
Expand Down Expand Up @@ -130,7 +130,7 @@ module RBS

def self.buffer: (String | Buffer source) -> Buffer

def self._parse_type: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed) -> Types::t?
def self._parse_type: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) -> Types::t?

def self._parse_method_type: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof) -> MethodType?

Expand Down
Loading