-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
borrowcheck error in code generated in a macro due to 2024 impl trait capture changes #132917
Comments
I don't think this is fixable |
That |
This also happens with an external crate that defines code with #[macro_export]
macro_rules! m {
() => {
pub struct X<'a>(::std::marker::PhantomData<&'a str>);
pub trait Tr<'a> {
fn foo(self, x: &mut X<'a>)
where
Self: Sized,
{
}
}
pub struct Y<'a>(::std::marker::PhantomData<&'a str>);
impl<'a> Tr<'a> for Y<'a> {}
pub fn f<'a>(x: &mut X<'a>) -> impl Tr<'a> {
Y(::std::marker::PhantomData)
}
pub fn g<'a>(x: &mut X<'a>) {
f(x).foo(x)
}
};
} |
@compiler-errors How does the compiler decide to use the new precise capturing rules? Does it look at the edition of the entire crate, or is there a specific span it is looking at? Or...? |
Looking closer, it looks like this is due to this (the switch of the edition here). @compiler-errors Can that be changed to not muck with the edition of the span? That is, apply something like this patch: diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs
index 5a0e9e8aec0..d53280751fc 100644
--- a/compiler/rustc_ast_lowering/src/lib.rs
+++ b/compiler/rustc_ast_lowering/src/lib.rs
@@ -738,7 +738,7 @@ fn mark_span_with_reason(
allow_internal_unstable: Option<Lrc<[Symbol]>>,
) -> Span {
self.tcx.with_stable_hashing_context(|hcx| {
- span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
+ span.mark_with_reason(allow_internal_unstable, reason, span.edition(), hcx)
})
} It doesn't seem correct to me to change the edition of a span. |
@ehuss: Yeah, that seems odd and comes from #50307, where afaict it doesn't seem intended. That would fix the macro by example version of this, but I don't believe it changes the proc macro version? I still think that that uses the span of the crate that calls the proc macro (which would be edition 2024). |
Ah, lol, the parse function uses call-site hygiene but that "call-site" span is still using the edition of the proc macro. How confusing. That's probably impossible to fix now, and probably debatable whether or not it should be fixed at all. @ehuss: Yeah, that fix you provided is both correct and likely not going to affect anything other than opaques today, which is probably why this is the first time we've seen it now. Feel free to put up a PR with the tests you've provided (maybe minimized), or I can do that instead. |
I tested with a proc-macro, and the patch above seems to fix it. [...deleted rest of comment seeing that you just posted...] |
I can post a PR. |
@lqd was mentioning this to me and we were wondering how related this could be with #132425 Was actually looking around for places where we do similar things and found ... diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs
index a825458dc89..1378639de17 100644
--- a/compiler/rustc_resolve/src/def_collector.rs
+++ b/compiler/rustc_resolve/src/def_collector.rs
@@ -200,7 +200,7 @@ fn visit_item(&mut self, i: &'a Item) {
ItemKind::Const(..) => DefKind::Const,
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
ItemKind::MacroDef(def) => {
- let edition = self.resolver.tcx.sess.edition();
+ let edition = i.span.edition();
let macro_data =
self.resolver.compile_macro(def, i.ident, &i.attrs, i.span, i.id, edition);
let macro_kind = macro_data.ext.macro_kind(); I wonder why don't we use |
…iler-errors Fix span edition for 2024 RPIT coming from an external macro This fixes a problem where code generated by an external macro with an RPIT would end up using the call-site edition instead of the macro's edition for the RPIT. When used from a 2024 crate, this caused the code to change behavior to the 2024 capturing rules, which we don't want. This was caused by the impl-trait lowering code would replace the span with one marked with `DesugaringKind::OpaqueTy` desugaring. However, it was also overriding the edition of the span with the edition of the local crate. Instead it should be using the edition of the span itself. Fixes rust-lang#132917
…iler-errors Fix span edition for 2024 RPIT coming from an external macro This fixes a problem where code generated by an external macro with an RPIT would end up using the call-site edition instead of the macro's edition for the RPIT. When used from a 2024 crate, this caused the code to change behavior to the 2024 capturing rules, which we don't want. This was caused by the impl-trait lowering code would replace the span with one marked with `DesugaringKind::OpaqueTy` desugaring. However, it was also overriding the edition of the span with the edition of the local crate. Instead it should be using the edition of the span itself. Fixes rust-lang#132917
…iler-errors Fix span edition for 2024 RPIT coming from an external macro This fixes a problem where code generated by an external macro with an RPIT would end up using the call-site edition instead of the macro's edition for the RPIT. When used from a 2024 crate, this caused the code to change behavior to the 2024 capturing rules, which we don't want. This was caused by the impl-trait lowering code would replace the span with one marked with `DesugaringKind::OpaqueTy` desugaring. However, it was also overriding the edition of the span with the edition of the local crate. Instead it should be using the edition of the span itself. Fixes rust-lang#132917
Rollup merge of rust-lang#133080 - ehuss:edition-desugar-span, r=compiler-errors Fix span edition for 2024 RPIT coming from an external macro This fixes a problem where code generated by an external macro with an RPIT would end up using the call-site edition instead of the macro's edition for the RPIT. When used from a 2024 crate, this caused the code to change behavior to the 2024 capturing rules, which we don't want. This was caused by the impl-trait lowering code would replace the span with one marked with `DesugaringKind::OpaqueTy` desugaring. However, it was also overriding the edition of the span with the edition of the local crate. Instead it should be using the edition of the span itself. Fixes rust-lang#132917
Code generated in a proc-macro in the 2021 edition may fail to compile when used in a 2024 crate due to changes in impl trait capturing. This will leave the author of the 2024 crate without any ability to fix the issue.
Example:
When the library is migrated with
cargo fix --edition
, there is a warning that the proc-macro has an issue, but with no detail on what's going on (since all the code is hidden):After migrating to 2024, it fails to compile:
Additionally, the suggestion provided there is incorrect, since it is not valid syntax.
I would expect the functions generated by the proc-macro to use the capturing rules from 2021. This is an issue because editions are supposed to be independent of the crate.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: