Skip to content
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

Emit single error for + use<'_> and don't suggest use<'static> #135052

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,15 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
LifetimeName::Param(def_id) => {
self.resolve_lifetime_ref(def_id, lt);
}
LifetimeName::Error => {}
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Infer
| LifetimeName::Static => {
LifetimeName::Infer | LifetimeName::Error => {
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
// We don't check for explicit `'_` lifetimes because we'll already have an
// explicit error from late resolution explaining `use<'_>` is invalid.
self.tcx.dcx().span_delayed_bug(
lt.ident.span,
"found infer or error lifetime but no prior error",
);
}
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Static => {
self.tcx.dcx().emit_err(errors::BadPreciseCapture {
span: lt.ident.span,
kind: "lifetime",
Expand Down
22 changes: 19 additions & 3 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,18 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r

fn visit_precise_capturing_arg(&mut self, arg: &'ast PreciseCapturingArg) {
match arg {
PreciseCapturingArg::Lifetime(lt) if lt.ident.name == kw::UnderscoreLifetime => {
// We account for `+ use<'_>` explicitly to avoid providing an invalid suggestion
// for `+ use<'static>`.
let outer_failures = take(&mut self.diag_metadata.current_elision_failures);
visit::walk_precise_capturing_arg(self, arg);
let elision_failures =
replace(&mut self.diag_metadata.current_elision_failures, outer_failures);
if !elision_failures.is_empty() {
self.report_missing_lifetime_specifiers(elision_failures, None, true);
}
return;
}
// Lower the lifetime regularly; we'll resolve the lifetime and check
// it's a parameter later on in HIR lowering.
PreciseCapturingArg::Lifetime(_) => {}
Expand Down Expand Up @@ -1877,7 +1889,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None, false);
}

#[instrument(level = "debug", skip(self))]
Expand Down Expand Up @@ -2097,7 +2109,11 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
LifetimeElisionCandidate::Ignore,
);
}
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
self.report_missing_lifetime_specifiers(
vec![missing_lifetime],
None,
false,
);
break;
}
LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstParamTy => {}
Expand Down Expand Up @@ -2220,7 +2236,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
replace(&mut self.diag_metadata.current_elision_failures, outer_failures);
if !elision_failures.is_empty() {
let Err(failure_info) = elision_lifetime else { bug!() };
self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info));
self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info), false);
}
}

Expand Down
19 changes: 14 additions & 5 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
&mut self,
lifetime_refs: Vec<MissingLifetime>,
function_param_lifetimes: Option<(Vec<MissingLifetime>, Vec<ElisionFnParameter>)>,
in_precise_capture: bool,
) -> ErrorGuaranteed {
let num_lifetimes: usize = lifetime_refs.iter().map(|lt| lt.count).sum();
let spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect();
Expand All @@ -3043,14 +3044,22 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
"missing lifetime specifier{}",
pluralize!(num_lifetimes)
);
self.add_missing_lifetime_specifiers_label(
&mut err,
lifetime_refs,
function_param_lifetimes,
);
if in_precise_capture {
err.note(
"this function's return type specifies a borrowed `use<'_>` with an elided \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a borrowed use<'_>" feels really awkward. I think this can be a bit more direct. Perhaps something like:

"cannot capture elided lifetime with use<'_> when there is no lifetime in scope to capture"

lifetime, but the lifetime cannot be derived from the arguments",
);
} else {
self.add_missing_lifetime_specifiers_label(
&mut err,
lifetime_refs,
function_param_lifetimes,
);
}
err.emit()
}

#[tracing::instrument(skip(self, err), level = "info")]
fn add_missing_lifetime_specifiers_label(
&mut self,
err: &mut Diag<'_>,
Expand Down
1 change: 0 additions & 1 deletion tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
fn no_elided_lt() -> impl Sized + use<'_> {}
//~^ ERROR missing lifetime specifier
//~| ERROR expected lifetime parameter in `use<...>` precise captures list, found `'_`

fn static_lt() -> impl Sized + use<'static> {}
//~^ ERROR expected lifetime parameter in `use<...>` precise captures list, found `'static`
Expand Down
20 changes: 5 additions & 15 deletions tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,25 @@ error[E0106]: missing lifetime specifier
--> $DIR/bad-lifetimes.rs:1:39
|
LL | fn no_elided_lt() -> impl Sized + use<'_> {}
| ^^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably at least keep some explanation that justifies why '_ isn't valid without another lifetime which it can resolve to in the inputs.

help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
| ^^
|
LL | fn no_elided_lt() -> impl Sized + use<'static> {}
| ~~~~~~~
= note: this function's return type specifies a borrowed `use<'_>` with an elided lifetime, but the lifetime cannot be derived from the arguments

error[E0261]: use of undeclared lifetime name `'missing`
--> $DIR/bad-lifetimes.rs:8:37
--> $DIR/bad-lifetimes.rs:7:37
|
LL | fn missing_lt() -> impl Sized + use<'missing> {}
| - ^^^^^^^^ undeclared lifetime
| |
| help: consider introducing lifetime `'missing` here: `<'missing>`

error: expected lifetime parameter in `use<...>` precise captures list, found `'_`
--> $DIR/bad-lifetimes.rs:1:39
|
LL | fn no_elided_lt() -> impl Sized + use<'_> {}
| ^^

error: expected lifetime parameter in `use<...>` precise captures list, found `'static`
--> $DIR/bad-lifetimes.rs:5:36
--> $DIR/bad-lifetimes.rs:4:36
|
LL | fn static_lt() -> impl Sized + use<'static> {}
| ^^^^^^^

error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0106, E0261.
For more information about an error, try `rustc --explain E0106`.
Loading