Repository navigation
import: builtin namespaces are automatic imports - #291
Merged
Merged
Conversation
A decode into a type with no constructor or shape of its own — a namespace such as JSON or Random, a native module's namespace, or a schema's object type — type-checked and then failed at runtime with "constructor for X not found". checkDecodeTarget now rejects it at compile time, at the top or in a field, for any such type. Signed-off-by: Alex Suraci <[email protected]>
JSON, YAML, TOML, Random and UUID (the static modules) are no longer Prelude objects and values. The import installer installs them as qualified-only imports, with an import origin, into a frame between the prelude and each module's scope, in type checking and evaluation alike. A module's declarations shadow them, and an import's namespace never holds them. An unqualified import name no longer overrides any import's namespace visible from the import's frame, which is what keeps a schema's `scalar JSON` from replacing the codec namespace. That replaces the isBuiltinValueName skip these names used to get: the rule is general, checked from value origins at inference and from per-frame marks at evaluation, with no name special-cased. Signed-off-by: Alex Suraci <[email protected]>
Describe JSON/YAML/TOML/Random/UUID as builtin imports (qualified only, shadowed by a module's declarations, never overridden by an import's unqualified names, not re-exported), and that a type with no constructor is not a decode target. Signed-off-by: Alex Suraci <[email protected]>
Review fixes for the decode-target check. A nullable or defaulted field of a type with no constructor decodes fine when the data leaves it out, so it is only rejected where every decoded value must become one. And a type declared in Dang always has a constructor, so one whose signature failed to check no longer adds a misleading no-constructor error. Signed-off-by: Alex Suraci <[email protected]>
A directory module's per-file import frame had no parent, so two file imports both exporting e.g. JSON installed it twice and made every unqualified JSON ambiguous. The frame now resolves through the builtin imports and prelude below the module, as a single file's imports do. Signed-off-by: Alex Suraci <[email protected]>
Describe that only required fields of a type with no constructor are rejected as decode targets, and the per-file import frames. Signed-off-by: Alex Suraci <[email protected]>
Review fixes for the import-namespace rule. Its runtime bookkeeping is
now a method set on the value scopes, so recordFieldScope inherits it
from its embedded overlay: an import in a block inside a record field
no longer replaces a builtin import at runtime. An import's unqualified
enum values follow the rule too, so `enum Format { JSON }` doesn't
replace the JSON namespace.
Signed-off-by: Alex Suraci <[email protected]>
Say that only non-null positions are rejected at compile time, and that a nullable one still fails at runtime when the data has a value. Signed-off-by: Alex Suraci <[email protected]>
A `let` of a builtin import's name shadows only the value; the type and its scoped directives still reach the builtin. Signed-off-by: Alex Suraci <[email protected]>
An explicit or auto-import named like a builtin import (`[imports.YAML]` plus `import YAML`) replaced it in a single file but not in a directory module or LoadDir session, where a file's imports are looked up after the module's chain, which held the builtin imports. Directory modules now give each file's import frame the builtin imports first, before its explicit imports, and keep them out of the module's own type and value chains, so the name resolves the same way in every mode, analysis included. Signed-off-by: Alex Suraci <[email protected]>
Describe that an import named like a builtin import replaces it in the file importing it, and where directory modules put the builtin imports. Signed-off-by: Alex Suraci <[email protected]>
Module.ValueEnv no longer binds the builtin imports (they are file imports in a directory module), so TypeEnv leaves them out too, as its doc says of imports: code checked against one runs in the other. Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
b48d3aa
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://8e2d2b1b.dang-3kk.pages.dev |
| Branch Preview URL: | https://builtin-namespace-imports.dang-3kk.pages.dev |
vito
force-pushed
the
builtin-namespace-imports
branch
from
November 10, 2026 03:09
ecb3eca to
b48d3aa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JSON,YAML,TOML,RandomandUUID(the static modules) become imports that Dang provides to every module, installed by the import installer, instead of Prelude objects and values. This is the "Q-41 B" follow-up to #276/#281. It also turns decoding into a type that has no constructor into a compile-time error.Builtin imports
They are installed with
installQualifiedImportTypeScope/installQualifiedImportValueScope, the qualified half of the ordinary import installer, before the module's explicit imports. They carry a qualified import origin, and nothing else checks their names.JSON.encode(x)works. There is no bareencode/decode/int.type JSON {…}orscalar JSONshadows the type and the value. Alet Random = …shadows the value only.[imports.YAML]plusimport YAML) and an auto-import config. Only the file importing it is affected; sibling files still get the builtin.Test.YAMLandLib.YAMLstay errors (import_qualified_codec,import_dang_qualified_codec).scalar JSONis reached asDagger.JSON, andJSONstays the codec namespace. This is a general rule, not a special case for these five names: an unqualified import name (a value, a type or an enum value) never overrides an import's namespace visible from the import's frame. That covers a builtin import, and an import in an enclosing scope of a nested block's import.resolvesToImportNamespace).installQualifiedImportValueScope(importNamespaceScope, implemented byObject,OverlayValueScopeand thereforerecordFieldScope, andConstructorScope).isBuiltinValueNameused to do for these names. Static modules are no longer builtin values, so that skip no longer covers them.Where they live
The builtin imports go wherever the module's explicit imports go. For each of their names, resolution is: the module's own declaration, then an import of that name, then the builtin import.
NewPreludeTypeScopeismodule → builtin imports → prelude, andNewValueScopemirrors it.LoadDirsessions: imports go in a per-file frame that is looked up after the module's declarations.newFileImportsScope, andinstallBuiltinImportValuesinevaluateDirectoryandNewSession.withoutBuiltinImportsinprepareFileScopes, andnewDirectoryValueScope. SoModule.TypeEnvandModule.ValueEnvagree on leaving them out, like any file import.YAMLresolve to whichever import that file sees.shadowedPreludeMemberHintalso consults the builtin imports, souser_scalar_json_shadows_codeckeeps its hint.Decode targets with no constructor
Decoding into
JSON!,Map[JSON!]!,Random!, a native module's namespace (Lib!) or a schema's object type (Test.ServerInfo!) used to type-check and then fail at runtime withconstructor for X not found. It is now rejected bycheckDecodeTarget, through a generichasDecodableShapecheck, wherever a decoded value must become one: a non-null target, non-null list/map elements, or a required field.cannot decode into JSON: it has no constructorcannot decode Holder.inner: its type Random has no constructor to decode intoThese positions still type-check:
Tests
pkg/dang/builtin_imports_test.gochecks:encode;JSONvs the builtin, at both levels;YAML, under RunDir, RunFile and LoadDir.tests/builtin_shadowing_test.go, run under RunDir and RunFile:JSON/YAML, or enum valuesTOML/UUID, don't override them, including in blocks and record fields;JSONaren't ambiguous;[imports.YAML]+import YAMLresolves to the module under RunDir, DeclareDir, RunFile and a LoadDir session, while a sibling file gets the builtin.analysis.TestImportReplacesBuiltinImport: Inspect resolvesYAMLto the import in the importing file and to the builtin in a sibling file.decode_into_namespace,decode_into_namespace_field,decode_into_module,decode_into_schema_object.test_json_import: an optional schema-object field still decodes.**) passes. I self-reviewed with the multi-agent reviewer over four rounds and fixed its findings.Release note
Builtin namespaces are imports.
JSON,YAML,TOML,RandomandUUIDare now imports that Dang provides to every module, not Prelude values.Module.TypeEnv/Module.ValueEnv(LoadDir) and the scopeRunDirreturns no longer include them, like any other file import.Decoding into a type with no constructor is a type error.
:: JSON!,:: Random!,Map[JSON!]!, a module namespace (Lib!) or a schema object type (Dagger.Container!) as a decode target, in a non-null position or a required field, now fails to compile:cannot decode into JSON: it has no constructor. It used to fail at runtime withconstructor for JSON not found. UseDecoded!for data of any shape. A nullable field of such a type still decodes when the data leaves it out.Not changed
DeclareDirstill evaluates every file's imports into the shared module frame at runtime. This was already the case, andTestDeclareDirSkipsBodiesAndKeepsDaggerTypesrelies on it. So in a module where one file importsYAML, a sibling file's function bodies would see that import if called from theDeclareDirscope. Type checking is per file, as in RunDir.Maintainer decisions