Skip to content

Commit cbe3d72

Browse files
committed
Warn if default arguments are given in protocol definitions, closes #15071
1 parent 44abf19 commit cbe3d72

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

lib/elixir/lib/protocol.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ defmodule Protocol do
287287
call_args = :lists.map(to_var, :lists.seq(2, arity))
288288
call_args = [quote(do: term) | call_args]
289289

290+
# TODO: Raise in Elixir v2.0
291+
if :lists.any(&match?({:\\, _, [_, _]}, &1), args) do
292+
IO.warn("default arguments in protocol definitions is deprecated", __CALLER__)
293+
end
294+
290295
quote generated: true do
291296
name = unquote(name)
292297
arity = unquote(arity)

lib/elixir/test/elixir/protocol_test.exs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -347,53 +347,34 @@ defmodule ProtocolTest do
347347
capture_io(:stderr, fn ->
348348
defprotocol SampleWithCallbacks do
349349
@spec with_specs(any(), keyword()) :: tuple()
350-
def with_specs(term, options \\ [])
350+
def with_specs(term, options)
351351

352352
@spec with_specs_and_when(any(), opts) :: tuple() when opts: keyword
353-
def with_specs_and_when(term, options \\ [])
353+
def with_specs_and_when(term, options)
354354

355355
def without_specs(term, options \\ [])
356356

357357
@callback foo :: {:ok, term}
358-
@callback foo(term) :: {:ok, term}
359-
@callback foo(term, keyword) :: {:ok, term, keyword}
360-
361-
@callback foo_when :: {:ok, x} when x: term
362358
@callback foo_when(x) :: {:ok, x} when x: term
363-
@callback foo_when(x, opts) :: {:ok, x, opts} when x: term, opts: keyword
364-
365359
@macrocallback bar(term) :: {:ok, term}
366-
@macrocallback bar(term, keyword) :: {:ok, term, keyword}
367360

368-
@optional_callbacks [foo: 1, foo: 2]
361+
@optional_callbacks [foo: 0]
369362
@optional_callbacks [without_specs: 2]
370363
end
371364
end)
372365

373366
assert message =~
374-
"cannot define @callback foo/0 inside protocol, use def/1 to outline your protocol definition"
375-
376-
assert message =~
377-
"cannot define @callback foo/1 inside protocol, use def/1 to outline your protocol definition"
378-
379-
assert message =~
380-
"cannot define @callback foo/2 inside protocol, use def/1 to outline your protocol definition"
367+
"default arguments in protocol definitions is deprecated"
381368

382369
assert message =~
383-
"cannot define @callback foo_when/0 inside protocol, use def/1 to outline your protocol definition"
370+
"cannot define @callback foo/0 inside protocol, use def/1 to outline your protocol definition"
384371

385372
assert message =~
386373
"cannot define @callback foo_when/1 inside protocol, use def/1 to outline your protocol definition"
387374

388-
assert message =~
389-
"cannot define @callback foo_when/2 inside protocol, use def/1 to outline your protocol definition"
390-
391375
assert message =~
392376
"cannot define @macrocallback bar/1 inside protocol, use def/1 to outline your protocol definition"
393377

394-
assert message =~
395-
"cannot define @macrocallback bar/2 inside protocol, use def/1 to outline your protocol definition"
396-
397378
assert message =~
398379
"cannot define @optional_callbacks inside protocol, all of the protocol definitions are required"
399380
end

0 commit comments

Comments
 (0)