{"record":{"id":"d1c2a97649d1b8d5","repo":"rust-lang/rust","slug":"expected-a-session","errorCode":null,"errorMessage":"expected a `Session`","messagePattern":"expected a `Session`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_builtin_macros/src/format.rs","lineNumber":655,"sourceCode":"                }\n                Width => (span, span),\n            };\n            let arg_name = args.explicit_args()[index].kind.ident().unwrap();\n            ecx.buffered_early_lint.push(BufferedEarlyLint {\n                span: Some(arg_name.span.into()),\n                node_id: rustc_ast::CRATE_NODE_ID,\n                lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),\n                diagnostic: DecorateDiagCompat(Box::new(move |dcx, level, sess| {\n                    let (suggestion, name) =\n                        if let Some(positional_arg_to_replace) = position_sp_to_replace {\n                            let mut name = arg_name.name.to_string();\n                            let is_formatting_arg = matches!(used_as, Width | Precision);\n                            if is_formatting_arg {\n                                name.push('$')\n                            };\n                            let span_to_replace = if let Ok(positional_arg_content) = sess\n                                .downcast_ref::<rustc_session::Session>()\n                                .expect(\"expected a `Session`\")\n                                .source_map()\n                                .span_to_snippet(positional_arg_to_replace)\n                                && positional_arg_content.starts_with(':')\n                            {\n                                positional_arg_to_replace.shrink_to_lo()\n                            } else {\n                                positional_arg_to_replace\n                            };\n                            (Some(span_to_replace), name)\n                        } else {\n                            (None, String::new())\n                        };\n\n                    diagnostics::NamedArgumentUsedPositionally {\n                        named_arg_sp: arg_name.span,\n                        position_label_sp: position_sp_for_msg,\n                        suggestion,\n                        name,","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_builtin_macros/src/format.rs#L637-L673","documentation":"This fires inside the `format!` macro expansion in rustc's builtin macros. When emitting the `NAMED_ARGUMENTS_USED_POSITIONALLY` lint diagnostic, the code accesses the diagnostic context's session via `sess.downcast_ref::<rustc_session::Session>().expect(...)`. It expects the diagnostic-providing session to be a `rustc_session::Session`. If the session is a different type (as can happen in non-standard rustc drivers, testing harnesses, or proc-macro servers), the downcast fails and panics.","triggerScenarios":"Using a `format!` macro where a named argument is also used positionally (triggering the lint), in a context where the diagnostic handler's session is not a `rustc_session::Session` — e.g., a custom rustc driver, a proc-macro server, or a test harness that provides a different session type to the diagnostic emitter.","commonSituations":"Building custom rustc tooling or drivers that reuse rustc's builtin macro expansion but provide their own session implementation. Also possible with edge cases in the format-args lint machinery after rustc internals refactor the session/diagnostic context type.","solutions":["If using a custom rustc driver, ensure your diagnostic handler wraps a real `rustc_session::Session` so the downcast succeeds.","Suppress the lint with `#![allow(named_arguments_used_positionally)]` at crate level to avoid triggering the diagnostic path entirely.","Update to the latest nightly — the diagnostic closure may be refactored to not need the session downcast.","File a bug: the diagnostic should use the provided `sess` generically rather than assuming the concrete `Session` type."],"exampleFix":"// before (triggers the lint → diagnostic path → session downcast)\nprintln!(\"{0} {x}\", \"hello\", x = \"world\");\n\n// after (avoid mixing named and positional, silencing the lint)\nprintln!(\"{0} {1}\", \"hello\", \"world\");\n// or\n#![allow(named_arguments_used_positionally)]","handlingStrategy":"validation","validationCode":"// As an end user, prevent the lint from firing by avoiding mixed\n// named/positional args in format strings:\n// Don't do: println!(\"{0} {x}\", val, x = other)\n// Do:       println!(\"{val} {x}\", val, x = other)\n// Or suppress at crate level:\n#![allow(named_arguments_used_positionally)]","typeGuard":null,"tryCatchPattern":"// In a custom rustc driver wrapping builtin macros:\nlet result = std::panic::catch_unwind(|| {\n    // expand format! macro here\n});\nif result.is_err() {\n    // session downcast failed; provide a fallback diagnostic\n    sess.warn(\"format args lint unavailable in this driver\");\n}","preventionTips":["Avoid mixing positional and named arguments in the same format string.","Add #![allow(named_arguments_used_positionally)] if using a custom rustc driver.","Custom rustc drivers should wrap a real rustc_session::Session.","Report the driver that triggers the downcast failure."],"tags":["rustc","builtin-macros","format","ice","lint","compiler-internal"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}