sorbet-runtime 0.5.9327 → 0.5.9338

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4086aef427b0d9ba64f92c95e703f259b44d1f0dd7c2cf3187d8873fdcd67db4
4
- data.tar.gz: 53e2599d56f09537dba517649a3b7672a0ce38eea85d10a2d1169d270e6472d1
3
+ metadata.gz: dd07f89d57f3da4bd05dc16bfe0163a67f417549c6e65771415df742fb02882f
4
+ data.tar.gz: 4e7650cda8f79b6a7bd0afc041ffbf9f2c07e596fb52fc38f8526f4b1cf723da
5
5
  SHA512:
6
- metadata.gz: 53955733aed646cd83ef989d79ec7d879f39afaa5cde54849972da5634c47d482441cf298509aa1f9d710d2873a37a16b65b051753d6ebc5b7aad686ea43cbbd
7
- data.tar.gz: 803cdd8a2273deda3ce67736224fceda59a6483a08cbe52cd7f55d85f2324c86e29748576e4e7dfe11ae6b39d70e99b024ec9285da443fd6e5c5d8d5fdb160d6
6
+ metadata.gz: 3dfb99200d94190aad5bd3ec27179b43a552c543c03c5be771de8b5985d1571c6e60e9ccc9ecf9b42a0bb1fe09f25ddca10e51a0e70b35cfca5a3fae27e19414
7
+ data.tar.gz: 31299aee649d7479933ea1b5eb3c1ae514279b02479e6d17f280753a964a5a0a6dd4da9feaeca83201e95cf7e807b302df7b4c31c5fa7f9460610e8a634b5f7c
@@ -22,6 +22,8 @@ require_relative 'types/private/decl_state'
22
22
  require_relative 'types/private/class_utils'
23
23
  require_relative 'types/private/runtime_levels'
24
24
  require_relative 'types/private/methods/_methods'
25
+ require_relative 'types/private/methods/modes'
26
+ require_relative 'types/private/methods/decl_builder'
25
27
  require_relative 'types/sig'
26
28
  require_relative 'types/helpers'
27
29
  require_relative 'types/private/final'
@@ -60,7 +62,6 @@ require_relative 'types/types/type_member'
60
62
  require_relative 'types/types/type_template'
61
63
 
62
64
  # Call validation
63
- require_relative 'types/private/methods/modes'
64
65
  require_relative 'types/private/methods/call_validation'
65
66
 
66
67
  # Signature validation
@@ -74,7 +75,6 @@ require_relative 'types/interface_wrapper'
74
75
  require_relative 'types/private/abstract/declare'
75
76
  require_relative 'types/private/abstract/hooks'
76
77
  require_relative 'types/private/casts'
77
- require_relative 'types/private/methods/decl_builder'
78
78
  require_relative 'types/private/methods/signature'
79
79
  require_relative 'types/private/retry'
80
80
  require_relative 'types/utils'
@@ -29,12 +29,13 @@ module T::Private::Methods
29
29
  ARG_NOT_PROVIDED = Object.new
30
30
  PROC_TYPE = Object.new
31
31
 
32
- DeclarationBlock = Struct.new(:mod, :loc, :blk, :final, :raw)
32
+ DeclarationBlock = Struct.new(:mod, :loc, :blk, :raw, :decl_builder)
33
33
 
34
34
  def self.declare_sig(mod, loc, arg, &blk)
35
- T::Private::DeclState.current.active_declaration = _declare_sig_internal(mod, loc, arg, &blk)
35
+ declaration = _declare_sig_internal(mod, loc, arg, &blk)
36
+ T::Private::DeclState.current.active_declaration = declaration
36
37
 
37
- nil
38
+ declaration.decl_builder
38
39
  end
39
40
 
40
41
  # See tests for how to use this. But you shouldn't be using this.
@@ -54,7 +55,10 @@ module T::Private::Methods
54
55
  raise "Invalid argument to `sig`: #{arg}"
55
56
  end
56
57
 
57
- DeclarationBlock.new(mod, loc, blk, arg == :final, raw)
58
+ decl_builder = DeclBuilder.new(mod, raw)
59
+ decl_builder.final if arg == :final # needed for backwards compatibility with sig(:final) {...}
60
+
61
+ DeclarationBlock.new(mod, loc, blk, raw, decl_builder)
58
62
  end
59
63
 
60
64
  def self._with_declared_signature(mod, declblock, &blk)
@@ -213,7 +217,7 @@ module T::Private::Methods
213
217
  current_declaration = T::Private::DeclState.current.active_declaration
214
218
  mod = is_singleton_method ? hook_mod.singleton_class : hook_mod
215
219
 
216
- if T::Private::Final.final_module?(mod) && (current_declaration.nil? || !current_declaration.final)
220
+ if T::Private::Final.final_module?(mod) && (current_declaration.nil? || !current_declaration.decl_builder.final?)
217
221
  raise "#{mod} was declared as final but its method `#{method_name}` was not declared as final"
218
222
  end
219
223
  # Don't compute mod.ancestors if we don't need to bother checking final-ness.
@@ -277,7 +281,7 @@ module T::Private::Methods
277
281
  end
278
282
 
279
283
  @sig_wrappers[key] = sig_block
280
- if current_declaration.final
284
+ if current_declaration.decl_builder.final?
281
285
  @was_ever_final_names[method_name] = true
282
286
  # use hook_mod, not mod, because for example, we want class C to be marked as having final if we def C.foo as
283
287
  # final. change this to mod to see some final_method tests fail.
@@ -346,10 +350,9 @@ module T::Private::Methods
346
350
  end
347
351
 
348
352
  def self.run_builder(declaration_block)
349
- builder = DeclBuilder.new(declaration_block.mod, declaration_block.raw)
350
- builder
351
- .instance_exec(&declaration_block.blk)
352
- .finalize!
353
+ declaration_block
354
+ .decl_builder
355
+ .run!(&declaration_block.blk)
353
356
  .decl
354
357
  end
355
358
 
@@ -2,9 +2,23 @@
2
2
  # typed: true
3
3
 
4
4
  module T::Private::Methods
5
- Declaration = Struct.new(:mod, :params, :returns, :bind, :mode, :checked, :finalized, :on_failure, :override_allow_incompatible, :type_parameters, :raw)
5
+ Declaration = Struct.new(
6
+ :mod,
7
+ :params,
8
+ :returns,
9
+ :bind,
10
+ :mode,
11
+ :checked,
12
+ :finalized,
13
+ :on_failure,
14
+ :override_allow_incompatible,
15
+ :type_parameters,
16
+ :raw,
17
+ :final
18
+ )
6
19
 
7
20
  class DeclBuilder
21
+ # The signature declaration the builder is composing (class `Declaration`)
8
22
  attr_reader :decl
9
23
 
10
24
  class BuilderError < StandardError; end
@@ -15,6 +29,24 @@ module T::Private::Methods
15
29
  end
16
30
  end
17
31
 
32
+ private def check_sig_block_is_unset!
33
+ if T::Private::DeclState.current.active_declaration&.blk
34
+ raise BuilderError.new(
35
+ "Cannot add more signature statements after the declaration block."
36
+ )
37
+ end
38
+ end
39
+
40
+ # Verify if we're trying to invoke the method outside of a signature block. Notice that we need to check if it's a
41
+ # proc, because this is valid and would lead to a false positive: `T.type_alias { T.proc.params(a: Integer).void }`
42
+ private def check_running_inside_block!(method_name)
43
+ unless @inside_sig_block || decl.mod == T::Private::Methods::PROC_TYPE
44
+ raise BuilderError.new(
45
+ "Can't invoke #{method_name} outside of a signature declaration block"
46
+ )
47
+ end
48
+ end
49
+
18
50
  def initialize(mod, raw)
19
51
  # TODO RUBYPLAT-1278 - with ruby 2.5, use kwargs here
20
52
  @decl = Declaration.new(
@@ -28,12 +60,23 @@ module T::Private::Methods
28
60
  ARG_NOT_PROVIDED, # on_failure
29
61
  nil, # override_allow_incompatible
30
62
  ARG_NOT_PROVIDED, # type_parameters
31
- raw
63
+ raw,
64
+ ARG_NOT_PROVIDED, # final
32
65
  )
66
+ @inside_sig_block = false
67
+ end
68
+
69
+ def run!(&block)
70
+ @inside_sig_block = true
71
+ instance_exec(&block)
72
+ finalize!
73
+ self
33
74
  end
34
75
 
35
76
  def params(**params)
36
77
  check_live!
78
+ check_running_inside_block!(__method__)
79
+
37
80
  if !decl.params.equal?(ARG_NOT_PROVIDED)
38
81
  raise BuilderError.new("You can't call .params twice")
39
82
  end
@@ -48,6 +91,8 @@ module T::Private::Methods
48
91
 
49
92
  def returns(type)
50
93
  check_live!
94
+ check_running_inside_block!(__method__)
95
+
51
96
  if decl.returns.is_a?(T::Private::Types::Void)
52
97
  raise BuilderError.new("You can't call .returns after calling .void.")
53
98
  end
@@ -62,6 +107,8 @@ module T::Private::Methods
62
107
 
63
108
  def void
64
109
  check_live!
110
+ check_running_inside_block!(__method__)
111
+
65
112
  if !decl.returns.equal?(ARG_NOT_PROVIDED)
66
113
  raise BuilderError.new("You can't call .void after calling .returns.")
67
114
  end
@@ -73,6 +120,8 @@ module T::Private::Methods
73
120
 
74
121
  def bind(type)
75
122
  check_live!
123
+ check_running_inside_block!(__method__)
124
+
76
125
  if !decl.bind.equal?(ARG_NOT_PROVIDED)
77
126
  raise BuilderError.new("You can't call .bind multiple times in a signature.")
78
127
  end
@@ -84,6 +133,7 @@ module T::Private::Methods
84
133
 
85
134
  def checked(level)
86
135
  check_live!
136
+ check_running_inside_block!(__method__)
87
137
 
88
138
  if !decl.checked.equal?(ARG_NOT_PROVIDED)
89
139
  raise BuilderError.new("You can't call .checked multiple times in a signature.")
@@ -102,6 +152,7 @@ module T::Private::Methods
102
152
 
103
153
  def on_failure(*args)
104
154
  check_live!
155
+ check_running_inside_block!(__method__)
105
156
 
106
157
  if !decl.on_failure.equal?(ARG_NOT_PROVIDED)
107
158
  raise BuilderError.new("You can't call .on_failure multiple times in a signature.")
@@ -115,7 +166,7 @@ module T::Private::Methods
115
166
  self
116
167
  end
117
168
 
118
- def abstract
169
+ def abstract(&blk)
119
170
  check_live!
120
171
 
121
172
  case decl.mode
@@ -127,15 +178,47 @@ module T::Private::Methods
127
178
  raise BuilderError.new("`.abstract` cannot be combined with `.override` or `.overridable`.")
128
179
  end
129
180
 
181
+ check_sig_block_is_unset!
182
+
183
+ if blk
184
+ T::Private::DeclState.current.active_declaration.blk = blk
185
+ end
186
+
130
187
  self
131
188
  end
132
189
 
133
- def final
190
+ def final(&blk)
134
191
  check_live!
135
- raise BuilderError.new("The syntax for declaring a method final is `sig(:final) {...}`, not `sig {final. ...}`")
192
+
193
+ if !decl.final.equal?(ARG_NOT_PROVIDED)
194
+ raise BuilderError.new("You can't call .final multiple times in a signature.")
195
+ end
196
+
197
+ if @inside_sig_block
198
+ raise BuilderError.new(
199
+ "Unlike other sig annotations, the `final` annotation must remain outside the sig block, " \
200
+ "using either `sig(:final) {...}` or `sig.final {...}`, not `sig {final. ...}"
201
+ )
202
+ end
203
+
204
+ raise BuilderError.new(".final cannot be repeated in a single signature") if final?
205
+
206
+ decl.final = true
207
+
208
+ check_sig_block_is_unset!
209
+
210
+ if blk
211
+ T::Private::DeclState.current.active_declaration.blk = blk
212
+ end
213
+
214
+ self
136
215
  end
137
216
 
138
- def override(allow_incompatible: false)
217
+ def final?
218
+ !decl.final.equal?(ARG_NOT_PROVIDED) && decl.final
219
+ end
220
+
221
+ def override(allow_incompatible: false, &blk)
139
222
  check_live!
140
223
 
141
224
  case decl.mode
@@ -150,10 +233,16 @@ module T::Private::Methods
150
233
  raise BuilderError.new("`.override` cannot be combined with `.abstract`.")
151
234
  end
152
235
 
236
+ check_sig_block_is_unset!
237
+
238
+ if blk
239
+ T::Private::DeclState.current.active_declaration.blk = blk
240
+ end
241
+
153
242
  self
154
243
  end
155
244
 
156
- def overridable
245
+ def overridable(&blk)
157
246
  check_live!
158
247
 
159
248
  case decl.mode
@@ -167,6 +256,12 @@ module T::Private::Methods
167
256
  raise BuilderError.new(".overridable cannot be repeated in a single signature")
168
257
  end
169
258
 
259
+ check_sig_block_is_unset!
260
+
261
+ if blk
262
+ T::Private::DeclState.current.active_declaration.blk = blk
263
+ end
264
+
170
265
  self
171
266
  end
172
267
 
@@ -183,6 +278,7 @@ module T::Private::Methods
183
278
  # def map(&blk); end
184
279
  def type_parameters(*names)
185
280
  check_live!
281
+ check_running_inside_block!(__method__)
186
282
 
187
283
  names.each do |name|
188
284
  raise BuilderError.new("not a symbol: #{name}") unless name.is_a?(Symbol)
data/lib/types/sig.rb CHANGED
@@ -12,10 +12,53 @@ module T::Sig
12
12
  original_verbose = $VERBOSE
13
13
  $VERBOSE = false
14
14
 
15
- # At runtime, does nothing, but statically it is treated exactly the same
16
- # as T::Sig#sig. Only use it in cases where you can't use T::Sig#sig.
15
+ # At runtime, only returns a fake declaration builder to allow chaining methods, but statically it is treated
16
+ # exactly the same as T::Sig#sig. Only use it in cases where you can't use T::Sig#sig.
17
17
  T::Sig::WithoutRuntime.sig {params(arg0: T.nilable(Symbol), blk: T.proc.bind(T::Private::Methods::DeclBuilder).void).void}
18
- def self.sig(arg0=nil, &blk); end # rubocop:disable Lint/DuplicateMethods
18
+ def self.sig(arg0=nil, &blk) # rubocop:disable Lint/DuplicateMethods
19
+ DeclBuilder.new
20
+ end
21
+
22
+ # This fake version of DeclBuilder exists so that signatures without runtime don't have to pay the cost associated
23
+ # with using the actual DeclBuilder
24
+ class DeclBuilder
25
+ T::Sig::WithoutRuntime.sig do
26
+ params(
27
+ _blk: T.nilable(T.proc.bind(T::Private::Methods::DeclBuilder).void)
28
+ ).returns(DeclBuilder)
29
+ end
30
+ def abstract(&_blk)
31
+ self
32
+ end
33
+
34
+ T::Sig::WithoutRuntime.sig do
35
+ params(
36
+ _blk: T.nilable(T.proc.bind(T::Private::Methods::DeclBuilder).void)
37
+ ).returns(DeclBuilder)
38
+ end
39
+ def final(&_blk)
40
+ self
41
+ end
42
+
43
+ T::Sig::WithoutRuntime.sig do
44
+ params(
45
+ _allow_incompatible: T::Boolean,
46
+ _blk: T.nilable(T.proc.bind(T::Private::Methods::DeclBuilder).void)
47
+ ).returns(DeclBuilder)
48
+ end
49
+ def override(_allow_incompatible: false, &_blk)
50
+ self
51
+ end
52
+
53
+ T::Sig::WithoutRuntime.sig do
54
+ params(
55
+ _blk: T.nilable(T.proc.bind(T::Private::Methods::DeclBuilder).void)
56
+ ).returns(DeclBuilder)
57
+ end
58
+ def overridable(&_blk)
59
+ self
60
+ end
61
+ end
19
62
 
20
63
  $VERBOSE = original_verbose
21
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9327
4
+ version: 0.5.9338
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-16 00:00:00.000000000 Z
11
+ date: 2021-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest