do not suggest precise capturing when the opaque span is inside a macro expansion

impl_trait_overcapture_suggestion anchored every suggestion on tcx.def_span(opaque_def_id) with no expansion check. for an opaque created inside an attribute macro expansion that span is the attribute, which is not a type position, so applying the machine-applicable suggestion breaks the file

the obvious patch of calling can_be_used_for_suggestions() on that span directly suppresses all suggestions including for ordinary code, because ast lowering stamps DesugaringKind::OpaqueTy onto the opaque span at compiler/rustc_ast_lowering/src/lib.rs:1825, making from_expansion() unconditionally true. the fix calls parent_callsite() to peel that single desugaring layer and asks can_be_used_for_suggestions() about the span underneath

the guard sits at function entry rather than beside the rpit_span push because the apit rename and the generics-list insertion are anchored on the same span, so one check covers all three

fixes https://github.com/rust-lang/rust/issues/139567
This commit is contained in:
albab-hasan
2026-10-01 09:58:45 +06:00
parent 1dae79f67e
commit 057e810a4e
2 changed files with 11 additions and 9 deletions

View File

@@ -1935,6 +1935,17 @@ pub fn impl_trait_overcapture_suggestion<'tcx>(
fn_def_id: LocalDefId,
captured_args: FxIndexSet<DefId>,
) -> Option<AddPreciseCapturingForOvercapture> {
let rpit_span = tcx.def_span(opaque_def_id);
// Every suggestion below is anchored on the opaque's span, so bail if that span is not one
// the user can edit. AST lowering marks it with the `impl Trait` desugaring, hence the
// question is about the span it desugared from: for an opaque created inside a macro
// expansion that span is the expansion site, which for an attribute macro is the attribute
// itself, where `+ use<..>` does not even parse.
if rpit_span.parent_callsite().is_some_and(|span| !span.can_be_used_for_suggestions()) {
return None;
}
let generics = tcx.generics_of(fn_def_id);
let mut captured_lifetimes = FxIndexSet::default();
@@ -2036,7 +2047,6 @@ pub fn impl_trait_overcapture_suggestion<'tcx>(
_ => ("", ""),
};
let rpit_span = tcx.def_span(opaque_def_id);
if !lparen.is_empty() {
suggs.push((rpit_span.shrink_to_lo(), lparen.to_string()));
}

View File

@@ -18,10 +18,6 @@ note: the lint level is defined here
LL | #![deny(impl_trait_overcaptures)]
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the attribute macro `overcapture_attr_pm::rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use the precise capturing `use<...>` syntax to make the captures explicit
|
LL | #[overcapture_attr_pm::rpc] + use<>
| +++++++
error: `impl Sized` will capture more lifetimes than possibly intended in edition 2024
--> $DIR/overcapture-suggestion-in-macro-issue-139567.rs:12:25
@@ -88,10 +84,6 @@ LL | mk_body!();
= note: all lifetimes in scope will be captured by `impl Trait`s in edition 2024
= warning: this changes meaning in Rust 2024
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/rpit-lifetime-capture.html>
help: use the precise capturing `use<...>` syntax to make the captures explicit
|
LL | pub fn from_macro_body(x: &u8) -> impl Sized + use<> { *x }
| +++++++
error: aborting due to 4 previous errors