Skip to content

Commit c44e930

Browse files
committed
Move report_elision_failure in diagnostics.rs.
1 parent 207c80f commit c44e930

File tree

2 files changed

+83
-82
lines changed

2 files changed

+83
-82
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+79
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,85 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18711871
err.emit();
18721872
}
18731873

1874+
/// Returns whether to add `'static` lifetime to the suggested lifetime list.
1875+
crate fn report_elision_failure(
1876+
&mut self,
1877+
db: &mut DiagnosticBuilder<'_>,
1878+
params: &[ElisionFailureInfo],
1879+
) -> bool {
1880+
let mut m = String::new();
1881+
let len = params.len();
1882+
1883+
let elided_params: Vec<_> =
1884+
params.iter().cloned().filter(|info| info.lifetime_count > 0).collect();
1885+
1886+
let elided_len = elided_params.len();
1887+
1888+
for (i, info) in elided_params.into_iter().enumerate() {
1889+
let ElisionFailureInfo { parent, index, lifetime_count: n, have_bound_regions, span } =
1890+
info;
1891+
1892+
db.span_label(span, "");
1893+
let help_name = if let Some(ident) =
1894+
parent.and_then(|body| self.tcx.hir().body(body).params[index].pat.simple_ident())
1895+
{
1896+
format!("`{}`", ident)
1897+
} else {
1898+
format!("argument {}", index + 1)
1899+
};
1900+
1901+
m.push_str(
1902+
&(if n == 1 {
1903+
help_name
1904+
} else {
1905+
format!(
1906+
"one of {}'s {} {}lifetimes",
1907+
help_name,
1908+
n,
1909+
if have_bound_regions { "free " } else { "" }
1910+
)
1911+
})[..],
1912+
);
1913+
1914+
if elided_len == 2 && i == 0 {
1915+
m.push_str(" or ");
1916+
} else if i + 2 == elided_len {
1917+
m.push_str(", or ");
1918+
} else if i != elided_len - 1 {
1919+
m.push_str(", ");
1920+
}
1921+
}
1922+
1923+
if len == 0 {
1924+
db.help(
1925+
"this function's return type contains a borrowed value, \
1926+
but there is no value for it to be borrowed from",
1927+
);
1928+
true
1929+
} else if elided_len == 0 {
1930+
db.help(
1931+
"this function's return type contains a borrowed value with \
1932+
an elided lifetime, but the lifetime cannot be derived from \
1933+
the arguments",
1934+
);
1935+
true
1936+
} else if elided_len == 1 {
1937+
db.help(&format!(
1938+
"this function's return type contains a borrowed value, \
1939+
but the signature does not say which {} it is borrowed from",
1940+
m
1941+
));
1942+
false
1943+
} else {
1944+
db.help(&format!(
1945+
"this function's return type contains a borrowed value, \
1946+
but the signature does not say whether it is borrowed from {}",
1947+
m
1948+
));
1949+
false
1950+
}
1951+
}
1952+
18741953
// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
18751954
// generics. We are disallowing this until we can decide on how we want to handle non-'static
18761955
// lifetimes in const generics. See issue #74052 for discussion.

compiler/rustc_resolve/src/late/lifetimes.rs

+4-82
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ enum Elide {
357357
#[derive(Clone, Debug)]
358358
crate struct ElisionFailureInfo {
359359
/// Where we can find the argument pattern.
360-
parent: Option<hir::BodyId>,
360+
crate parent: Option<hir::BodyId>,
361361
/// The index of the argument in the original definition.
362-
index: usize,
363-
lifetime_count: usize,
364-
have_bound_regions: bool,
362+
crate index: usize,
363+
crate lifetime_count: usize,
364+
crate have_bound_regions: bool,
365365
crate span: Span,
366366
}
367367

@@ -3166,84 +3166,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
31663166
err.emit();
31673167
}
31683168

3169-
fn report_elision_failure(
3170-
&mut self,
3171-
db: &mut DiagnosticBuilder<'_>,
3172-
params: &[ElisionFailureInfo],
3173-
) -> bool /* add `'static` lifetime to lifetime list */ {
3174-
let mut m = String::new();
3175-
let len = params.len();
3176-
3177-
let elided_params: Vec<_> =
3178-
params.iter().cloned().filter(|info| info.lifetime_count > 0).collect();
3179-
3180-
let elided_len = elided_params.len();
3181-
3182-
for (i, info) in elided_params.into_iter().enumerate() {
3183-
let ElisionFailureInfo { parent, index, lifetime_count: n, have_bound_regions, span } =
3184-
info;
3185-
3186-
db.span_label(span, "");
3187-
let help_name = if let Some(ident) =
3188-
parent.and_then(|body| self.tcx.hir().body(body).params[index].pat.simple_ident())
3189-
{
3190-
format!("`{}`", ident)
3191-
} else {
3192-
format!("argument {}", index + 1)
3193-
};
3194-
3195-
m.push_str(
3196-
&(if n == 1 {
3197-
help_name
3198-
} else {
3199-
format!(
3200-
"one of {}'s {} {}lifetimes",
3201-
help_name,
3202-
n,
3203-
if have_bound_regions { "free " } else { "" }
3204-
)
3205-
})[..],
3206-
);
3207-
3208-
if elided_len == 2 && i == 0 {
3209-
m.push_str(" or ");
3210-
} else if i + 2 == elided_len {
3211-
m.push_str(", or ");
3212-
} else if i != elided_len - 1 {
3213-
m.push_str(", ");
3214-
}
3215-
}
3216-
3217-
if len == 0 {
3218-
db.help(
3219-
"this function's return type contains a borrowed value, \
3220-
but there is no value for it to be borrowed from",
3221-
);
3222-
true
3223-
} else if elided_len == 0 {
3224-
db.help(
3225-
"this function's return type contains a borrowed value with \
3226-
an elided lifetime, but the lifetime cannot be derived from \
3227-
the arguments",
3228-
);
3229-
true
3230-
} else if elided_len == 1 {
3231-
db.help(&format!(
3232-
"this function's return type contains a borrowed value, \
3233-
but the signature does not say which {} it is borrowed from",
3234-
m
3235-
));
3236-
false
3237-
} else {
3238-
db.help(&format!(
3239-
"this function's return type contains a borrowed value, \
3240-
but the signature does not say whether it is borrowed from {}",
3241-
m
3242-
));
3243-
false
3244-
}
3245-
}
3246-
32473169
fn resolve_object_lifetime_default(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
32483170
debug!("resolve_object_lifetime_default(lifetime_ref={:?})", lifetime_ref);
32493171
let mut late_depth = 0;

0 commit comments

Comments
 (0)