{"id":"01479b1be1cf4a96","repo":"rust-lang/rust","slug":"attempted-to-encode-lazyattrtokenstream","errorCode":null,"errorMessage":"Attempted to encode LazyAttrTokenStream","messagePattern":"Attempted to encode LazyAttrTokenStream","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_ast/src/tokenstream.rs","lineNumber":150,"sourceCode":"            break_last_token,\n            node_replacements,\n        }))\n    }\n\n    pub fn to_attr_token_stream(&self) -> AttrTokenStream {\n        self.0.to_attr_token_stream()\n    }\n}\n\nimpl fmt::Debug for LazyAttrTokenStream {\n    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {\n        write!(f, \"LazyAttrTokenStream({:?})\", self.to_attr_token_stream())\n    }\n}\n\nimpl<S: SpanEncoder> Encodable<S> for LazyAttrTokenStream {\n    fn encode(&self, _s: &mut S) {\n        panic!(\"Attempted to encode LazyAttrTokenStream\");\n    }\n}\n\nimpl<D: SpanDecoder> Decodable<D> for LazyAttrTokenStream {\n    fn decode(_d: &mut D) -> Self {\n        panic!(\"Attempted to decode LazyAttrTokenStream\");\n    }\n}\n\nimpl StableHash for LazyAttrTokenStream {\n    fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {\n        panic!(\"Attempted to compute stable hash for LazyAttrTokenStream\");\n    }\n}\n\n/// A token range within a `Parser`'s full token stream.\n#[derive(Clone, Debug)]\npub struct ParserRange(pub Range<u32>);","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_ast/src/tokenstream.rs#L132-L168","documentation":"`panic!(\"Attempted to encode LazyAttrTokenStream\")` is fired by the `Encodable` impl for `LazyAttrTokenStream`. The lazy stream holds parser-internal cursor state (snapshots, call counts, node replacements) that is intentionally non-serializable; it must be *forced* into a concrete `AttrTokenStream` before it can be encoded to incr-comp data or cached. The panic exists to catch serialization of un-forced lazy streams.","triggerScenarios":"Any `Encodable::encode` call (directly or via `rustc_serialize`, incremental compilation serialization, or query caching) on a value still typed as `LazyAttrTokenStream`. This happens when a token stream wasn't forced to `AttrTokenStream` before being placed in a serializable struct.","commonSituations":"Refactors that change when lazy streams are forced, regressions in attribute/macro token collection, or new query results that retain `LazyAttrTokenStream` past the parse phase. Typically surfaces as an ICE during incremental compilation or crate serialization.","solutions":["Call `.to_attr_token_stream()` (or otherwise force) the `LazyAttrTokenStream` into a concrete `AttrTokenStream` before storing it in any `Encodable` structure.","Audit the call site in the backtrace (`RUST_BACKTRACE=1`) to find which field still holds a `LazyAttrTokenStream` at serialization time, and force it at construction.","Add a unit test that round-trips the affected AST node through `Encodable`/`Decodable` to prevent regression."],"exampleFix":"// before\nattr_tokens: LazyAttrTokenStream::new(...)\n// later: encode fails\n\n// after\nattr_tokens: lazy.to_attr_token_stream()  // forced before storage","handlingStrategy":"validation","validationCode":"// LazyAttrTokenStream is intentionally non-Encodable.\n// Materialize to a real stream before encoding:\nlet encodable: AttrTokenStream = lazy.to_attr_token_stream();\n// then encode encodable instead","typeGuard":"fn is_lazy_stream<T>(_v: &T) -> bool { false } // types-other-than-LazyAttrTokenStream are safe","tryCatchPattern":null,"preventionTips":["Never run `Encodable::encode` on a `LazyAttrTokenStream`; the impl is a hard panic.","Before any serialization pipeline, call `.to_attr_token_stream()` to force evaluation.","If you must walk a token stream for hashing/encoding, work on `AttrTokenStream`, not its lazy variant."],"tags":["rustc","internals","tokenstream","serialization","incr-comp"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}