Rollup merge of #130299 - vetleras:add_set_dcx, r=compiler-errors

Add set_dcx to ParseSess

After [this](https://github.com/rust-lang/rust/pull/126623) PR was merged, it is no longer possible to inject one's own `Emitter` in the way [described in the Compiler Development Guide](https://rustc-dev-guide.rust-lang.org/rustc-driver-getting-diagnostics.html). The reason is that the `dcx` field in `ParseSess` is no longer public, so it is not possible to update the `dcx` field with a `DiagCtxt` that contains one's own `Emitter` in the `psess_created` callback in `rustc_interface::Config`. The only way I have found to insert my own `DiagCtxt` is by creating an entirely new `ParseSess` and replacing the old one. This is not a good solution as the original `ParseSess` contains fields I would like to keep. (In my case the problem is that I lose the `cfg` and `check-cfg` fields of the original.)

The solution proposed in this PR is to add a `set_dcx` method to `ParseSess`. Per my limited understanding of the rustc codebase this should be fine as `set_dcx` requires a mutable reference to `ParseSess`, which is as far as I know only available in the `psess_created` callback (outside of `rustc_interface::run_compiler`).

If this PR is accepted, I will create a new PR to update the aforementioned example in the Compiler Development Guide.
This commit is contained in:
Matthias Krüger 2024-09-13 18:25:46 +02:00 committed by GitHub
commit b0e323549a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -340,4 +340,8 @@ pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> + '
pub fn dcx(&self) -> DiagCtxtHandle<'_> { pub fn dcx(&self) -> DiagCtxtHandle<'_> {
self.dcx.handle() self.dcx.handle()
} }
pub fn set_dcx(&mut self, dcx: DiagCtxt) {
self.dcx = dcx;
}
} }