Skip to content

Commit 6124608

Browse files
lukaszsamsonclaude
andcommitted
Tighten stdlib typespecs to match implementations
Candidates found by cross-checking @specs against the type signatures inferred by the compiler (see #15559): these specs declared strictly wider return types than the functions can produce, where the gap carries no intentional API room (deliberately abstract contracts, such as atom() covering future values, are left untouched): * File.Stream.t/0: type the struct fields (mirrors IO.Stream.t/0) * Map.new/0: %{} instead of map() * Logger.levels/0: :logger.level() excludes :warn, which is only an accepted input alias and is never returned * Macro.Env.location/1 and Macro.Env.stacktrace/1: precise shapes Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 251d3fe commit 6124608

5 files changed

Lines changed: 12 additions & 6 deletions

File tree

lib/elixir/lib/file/stream.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ defmodule File.Stream do
1818

1919
defstruct path: nil, modes: [], line_or_bytes: :line, raw: true, node: nil
2020

21-
@type t :: %__MODULE__{}
21+
@type t :: %__MODULE__{
22+
path: Path.t(),
23+
modes: [term()],
24+
line_or_bytes: :line | pos_integer(),
25+
raw: boolean(),
26+
node: node()
27+
}
2228

2329
@doc false
2430
def __build__(path, line_or_bytes, modes) do

lib/elixir/lib/macro/env.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ defmodule Macro.Env do
203203
Returns a keyword list containing the file and line
204204
information as keys.
205205
"""
206-
@spec location(t) :: keyword
206+
@spec location(t) :: [file: file, line: line]
207207
def location(env)
208208

209209
def location(%{__struct__: Macro.Env, file: file, line: line}) do
@@ -700,7 +700,7 @@ defmodule Macro.Env do
700700
@doc """
701701
Returns the environment stacktrace.
702702
"""
703-
@spec stacktrace(t) :: list
703+
@spec stacktrace(t) :: [{module, atom, arity, keyword}]
704704
def stacktrace(%{__struct__: Macro.Env} = env) do
705705
cond do
706706
is_nil(env.module) ->

lib/elixir/lib/map.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ defmodule Map do
201201
%{}
202202
203203
"""
204-
@spec new :: map
204+
@spec new :: %{}
205205
def new, do: %{}
206206

207207
@doc """

lib/elixir/lib/module.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ defmodule Module do
754754
755755
"""
756756
@doc since: "1.12.0"
757-
@spec reserved_attributes() :: map
757+
@spec reserved_attributes() :: %{optional(atom()) => %{doc: binary()}}
758758
def reserved_attributes() do
759759
%{
760760
after_compile: %{

lib/logger/lib/logger.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ defmodule Logger do
537537
Returns all the available levels.
538538
"""
539539
@doc since: "1.16.0"
540-
@spec levels() :: [level(), ...]
540+
@spec levels() :: [:logger.level(), ...]
541541
def levels(), do: @levels
542542

543543
@doc ~S"""

0 commit comments

Comments
 (0)