Skip to content

Commit 7bf026a

Browse files
committed
fix: runtime type errors found by Sorbet
1 parent ad273ed commit 7bf026a

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

lib/arch_obj_models/csr.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,14 @@ def sw_read_ast(symtab)
475475
return_type: Idl::Type.new(:bits, width: 128), # big int to hold special return values
476476
name: "CSR[#{name}].sw_read()",
477477
input_file: __source,
478-
input_line: source_line("sw_read()"),
478+
input_line: source_line(["sw_read()"]),
479479
symtab:,
480480
type_check: false
481481
)
482482

483483
raise "unexpected #{@sw_read_ast.class}" unless @sw_read_ast.is_a?(Idl::FunctionBodyAst)
484484

485-
@sw_read_ast.set_input_file_unless_already_set(__source, source_line("sw_read()"))
485+
@sw_read_ast.set_input_file_unless_already_set(__source, source_line(["sw_read()"]))
486486

487487
@sw_read_ast
488488
end

lib/arch_obj_models/csr_field.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def initialize(parent_csr, field_name, field_data)
4343
def __source = @parent.__source
4444

4545
# CSR field data starts at fields: NAME: with the YAML
46-
def source_line(*path)
47-
T.unsafe(self).super("fields", name, *path)
46+
sig { params(path: T::Array[String]).returns(Integer) }
47+
def source_line(path)
48+
super(["fields", name].concat(path))
4849
end
4950

5051
# For a full config, whether or not the field is implemented
@@ -104,7 +105,7 @@ def type_ast
104105
idl_code,
105106
name: "CSR[#{csr.name}].#{name}.type()",
106107
input_file: csr.__source,
107-
input_line: csr.source_line("fields", name, "type()"),
108+
input_line: csr.source_line(["fields", name, "type()"]),
108109
symtab: @cfg_arch.symtab,
109110
type_check: false
110111
)
@@ -346,7 +347,7 @@ def reset_value_ast
346347
return_type: Idl::Type.new(:bits, width: max_width),
347348
name: "CSR[#{parent.name}].#{name}.reset_value()",
348349
input_file: csr.__source,
349-
input_line: csr.source_line("fields", name, "reset_value()"),
350+
input_line: csr.source_line(["fields", name, "reset_value()"]),
350351
symtab: cfg_arch.symtab,
351352
type_check: false
352353
)
@@ -497,7 +498,7 @@ def sw_write_ast(symtab)
497498
return_type: Idl::Type.new(:bits, width: 128), # big int to hold special return values
498499
name: "CSR[#{csr.name}].#{name}.sw_write(csr_value)",
499500
input_file: csr.__source,
500-
input_line: csr.source_line("fields", name, "sw_write(csr_value)"),
501+
input_line: csr.source_line(["fields", name, "sw_write(csr_value)"]),
501502
symtab:,
502503
type_check: false
503504
)
@@ -822,6 +823,6 @@ def location_pretty(effective_xlen = nil)
822823
# @return [String] Long description of the field type
823824
sig { params(effective_xlen: T.nilable(Integer)).returns(String) }
824825
def type_desc(effective_xlen=nil)
825-
TYPE_DESC_MAP[type(effective_xlen)]
826+
TYPE_DESC_MAP.fetch(type(effective_xlen), "")
826827
end
827828
end

lib/arch_obj_models/database_obj.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,14 @@ class ValidationError < ::StandardError
123123
# @return [ConfiguredArchitecture] If a specification and config is known
124124
# @return [nil] Otherwise
125125
sig { returns(ConfiguredArchitecture) }
126-
attr_reader :cfg_arch # Use when extra stuff provided by ConfiguredArchitecture is required
126+
def cfg_arch
127+
raise "no cfg_arch" if @cfg_arch.nil?
128+
129+
@cfg_arch
130+
end
131+
132+
sig { returns(T::Boolean) }
133+
def cfg_arch? = !@cfg_arch.nil?
127134

128135
sig { returns(String) }
129136
def kind = @data["kind"]
@@ -249,7 +256,7 @@ def description(
249256
symtab: @cfg_arch.symtab,
250257
name: "#{name}.description[#{idx}].when",
251258
input_file: __source,
252-
input_line: source_line("description", idx, "when()")
259+
input_line: source_line(["description", idx, "when()"])
253260
)
254261

255262
symtab = @cfg_arch.symtab.global_clone
@@ -362,8 +369,8 @@ def primary_defined_by
362369
# 06: YAML
363370
# misa_csr.source_line("sw_read()") #=> 2
364371
# mis_csr.source_line("fields", "A", "type()") #=> 5
365-
sig { params(path: String).returns(Integer) }
366-
def source_line(*path)
372+
sig { params(path: T::Array[String]).returns(Integer) }
373+
def source_line(path)
367374

368375
# find the line number of this operation() in the *original* file
369376
yaml_filename = __source

lib/arch_obj_models/instruction.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def operation_ast
761761
self,
762762
symtab: cfg_arch.symtab,
763763
input_file: @data["$source"],
764-
input_line: source_line("operation()")
764+
input_line: source_line(["operation()"])
765765
)
766766

767767
raise "unexpected #{ast.class}" unless ast.is_a?(Idl::FunctionBodyAst)

lib/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def initialize(portfolio_grp)
320320
@portfolio_grp = portfolio_grp
321321

322322
portfolio_grp.portfolios.each do |portfolio|
323-
raise "Portfolio #{portfolio.name} shouldn't have a non-nil cfg_arch member" unless portfolio.cfg_arch.nil?
323+
raise "Portfolio #{portfolio.name} shouldn't have a non-nil cfg_arch member" if portfolio.cfg_arch?
324324
raise "Portfolio #{portfolio.name} shouldn't have a an arch member of type ConfiguredArchitecture" if portfolio.arch.is_a?(ConfiguredArchitecture)
325325
end
326326
end

0 commit comments

Comments
 (0)