Simplify lowering.

This commit is contained in:
Camille GILLOT 2021-10-31 17:54:47 +01:00
parent 5ea7ea8a57
commit b621133200

View file

@ -268,7 +268,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let has_lifetimes = let has_lifetimes =
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_))); generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
if !generic_args.parenthesized && !has_lifetimes { if !generic_args.parenthesized && !has_lifetimes && expected_lifetimes > 0 {
// Note: these spans are used for diagnostics when they can't be inferred. // Note: these spans are used for diagnostics when they can't be inferred.
// See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label // See rustc_resolve::late::lifetimes::LifetimeContext::add_missing_lifetime_specifiers_label
let elided_lifetime_span = if generic_args.span.is_empty() { let elided_lifetime_span = if generic_args.span.is_empty() {
@ -286,49 +286,43 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.map(GenericArg::Lifetime) .map(GenericArg::Lifetime)
.chain(generic_args.args.into_iter()) .chain(generic_args.args.into_iter())
.collect(); .collect();
if expected_lifetimes > 0 && param_mode == ParamMode::Explicit { // In create-parameter mode we error here because we don't want to support
// deprecated impl elision in new features like impl elision and `async fn`,
// both of which work using the `CreateParameter` mode:
//
// impl Foo for std::cell::Ref<u32> // note lack of '_
// async fn foo(_: std::cell::Ref<u32>) { ... }
if let (ParamMode::Explicit, AnonymousLifetimeMode::CreateParameter) =
(param_mode, self.anonymous_lifetime_mode)
{
let anon_lt_suggestion = vec!["'_"; expected_lifetimes].join(", "); let anon_lt_suggestion = vec!["'_"; expected_lifetimes].join(", ");
let no_non_lt_args = generic_args.args.len() == expected_lifetimes; let no_non_lt_args = generic_args.args.len() == expected_lifetimes;
let no_bindings = generic_args.bindings.is_empty(); let no_bindings = generic_args.bindings.is_empty();
let (incl_angl_brckt, insertion_sp, suggestion) = if no_non_lt_args && no_bindings { let (incl_angl_brckt, suggestion) = if no_non_lt_args && no_bindings {
// If there are no generic args, our suggestion can include the angle brackets. // If there are no generic args, our suggestion can include the angle brackets.
(true, path_span.shrink_to_hi(), format!("<{}>", anon_lt_suggestion)) (true, format!("<{}>", anon_lt_suggestion))
} else { } else {
// Otherwise we'll insert a `'_, ` right after the opening bracket. // Otherwise we'll insert a `'_, ` right after the opening bracket.
let span = generic_args (false, format!("{}, ", anon_lt_suggestion))
.span
.with_lo(generic_args.span.lo() + BytePos(1))
.shrink_to_lo();
(false, span, format!("{}, ", anon_lt_suggestion))
}; };
match self.anonymous_lifetime_mode { let insertion_sp = elided_lifetime_span.shrink_to_hi();
// In create-parameter mode we error here because we don't want to support let mut err = struct_span_err!(
// deprecated impl elision in new features like impl elision and `async fn`, self.sess,
// both of which work using the `CreateParameter` mode: path_span,
// E0726,
// impl Foo for std::cell::Ref<u32> // note lack of '_ "implicit elided lifetime not allowed here"
// async fn foo(_: std::cell::Ref<u32>) { ... } );
AnonymousLifetimeMode::CreateParameter => { rustc_errors::add_elided_lifetime_in_path_suggestion(
let mut err = struct_span_err!( &self.sess.source_map(),
self.sess, &mut err,
path_span, expected_lifetimes,
E0726, path_span,
"implicit elided lifetime not allowed here" incl_angl_brckt,
); insertion_sp,
rustc_errors::add_elided_lifetime_in_path_suggestion( suggestion,
&self.sess.source_map(), );
&mut err, err.note("assuming a `'static` lifetime...");
expected_lifetimes, err.emit();
path_span,
incl_angl_brckt,
insertion_sp,
suggestion,
);
err.note("assuming a `'static` lifetime...");
err.emit();
}
AnonymousLifetimeMode::PassThrough | AnonymousLifetimeMode::ReportError => {}
}
} }
} }