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

Rollup of 5 pull requests #97637

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8da2707
Stop pretty-printing anonymous lifetimes.
cjgillot May 12, 2022
da175c7
Make lifetime errors more precise in the presence of `Fresh` lifetimes.
cjgillot May 12, 2022
7b86c6f
Fortify check for number of generic parameters.
cjgillot May 12, 2022
3162b33
Handle anonymous lifetimes properly in diagnostics.
cjgillot May 13, 2022
0cf79d7
Rebase fallout.
cjgillot May 29, 2022
bd4d1cd
migrate `maybe_recover_from_bad_qpath_stage_2` diagnostic
pvdrz May 31, 2022
29ed9a5
migrate `maybe_consume_incorrect_semicolon` diagnostic
pvdrz May 31, 2022
93a427e
migrate `recover_from_await_method_call` diagnostic
pvdrz May 31, 2022
2a0496c
use `suggestion_short` for incorrect semicolon diagnostic
pvdrz May 31, 2022
a06ba45
migrate `error_on_incorrect_await` diagnostic
pvdrz May 31, 2022
9ce04e3
merge diagnostics about incorrect uses of `.await`
pvdrz May 31, 2022
e1d63d1
migrate `check_for_for_in_in_typo` diagnostic
pvdrz May 31, 2022
0fa70a8
rename `sp` to `span`
pvdrz Jun 1, 2022
410ffe3
Add Symbol into rustdoc JSON ID to prevent conflicts between reexports
GuillaumeGomez May 31, 2022
069ae16
Add regression test for json reexport bug
GuillaumeGomez May 31, 2022
572c390
Stabilize `box_into_pin`
JohnTitor May 25, 2022
77e1069
Revert #96682.
nnethercote Jun 2, 2022
0ed450b
Rollup merge of #97023 - cjgillot:uniform-anon, r=estebank
Dylan-DPC Jun 2, 2022
78cd67b
Rollup merge of #97397 - JohnTitor:stabilize-box-into-pin, r=Mark-Sim…
Dylan-DPC Jun 2, 2022
6ee7bd3
Rollup merge of #97587 - pvdrz:maybe-recover-from-bad-qpath-stage-2, …
Dylan-DPC Jun 2, 2022
15c960b
Rollup merge of #97599 - GuillaumeGomez:reexport-json, r=CraftSpider
Dylan-DPC Jun 2, 2022
249ffc0
Rollup merge of #97636 - nnethercote:revert-96682, r=dtolnay
Dylan-DPC Jun 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/rustc_ast/src/token.rs
Expand Up @@ -50,12 +50,11 @@ pub enum Delimiter {
Brace,
/// `[ ... ]`
Bracket,
/// `/*«*/ ... /*»*/`
/// `Ø ... Ø`
/// An invisible delimiter, that may, for example, appear around tokens coming from a
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
/// `$var * 3` where `$var` is `1 + 2`.
/// Invisible delimiters are not directly writable in normal Rust code except as comments.
/// Therefore, they might not survive a roundtrip of a token stream through a string.
/// Invisible delimiters might not survive roundtrip of a token stream through a string.
Invisible,
}

Expand Down
23 changes: 5 additions & 18 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Expand Up @@ -590,28 +590,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.nbsp();
}
self.word("{");
let empty = tts.is_empty();
if !empty {
if !tts.is_empty() {
self.space();
}
self.ibox(0);
self.print_tts(tts, convert_dollar_crate);
self.end();
self.bclose(span, empty);
}
Some(Delimiter::Invisible) => {
self.word("/*«*/");
let empty = tts.is_empty();
if !empty {
self.space();
}
self.ibox(0);
self.print_tts(tts, convert_dollar_crate);
self.end();
if !empty {
self.space();
}
self.word("/*»*/");
self.bclose(span, empty);
}
Some(delim) => {
let token_str = self.token_kind_to_string(&token::OpenDelim(delim));
Expand Down Expand Up @@ -786,8 +772,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
token::CloseDelim(Delimiter::Bracket) => "]".into(),
token::OpenDelim(Delimiter::Brace) => "{".into(),
token::CloseDelim(Delimiter::Brace) => "}".into(),
token::OpenDelim(Delimiter::Invisible) => "/*«*/".into(),
token::CloseDelim(Delimiter::Invisible) => "/*»*/".into(),
token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible) => {
"".into()
}
token::Pound => "#".into(),
token::Dollar => "$".into(),
token::Question => "?".into(),
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Expand Up @@ -567,15 +567,17 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
let lifetime =
self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?;
match lifetime.name {
hir::LifetimeName::Param(_)
hir::LifetimeName::Param(hir::ParamName::Plain(_) | hir::ParamName::Error)
| hir::LifetimeName::Error
| hir::LifetimeName::Static
| hir::LifetimeName::Underscore => {
| hir::LifetimeName::Static => {
let lifetime_span = lifetime.span;
Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span))
}

hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => {
hir::LifetimeName::Param(hir::ParamName::Fresh(_))
| hir::LifetimeName::ImplicitObjectLifetimeDefault
| hir::LifetimeName::Implicit
| hir::LifetimeName::Underscore => {
// In this case, the user left off the lifetime; so
// they wrote something like:
//
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Expand Up @@ -14,3 +14,21 @@ parser-add-paren = try adding parentheses
parser-forgot-paren = perhaps you forgot parentheses?

parser-expect-path = expected a path

parser-maybe-recover-from-bad-qpath-stage-2 =
missing angle brackets in associated item path
.suggestion = try: `{$ty}`

parser-incorrect-semicolon =
expected item, found `;`
.suggestion = remove this semicolon
.help = {$name} declarations are not followed by a semicolon

parser-incorrect-use-of-await =
incorrect use of `await`
.parentheses-suggestion = `await` is not a method call, remove the parentheses
.postfix-suggestion = `await` is a postfix operation

parser-in-in-typo =
expected iterable, found keyword `in`
.suggestion = remove the duplicated `in`
11 changes: 11 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Expand Up @@ -131,6 +131,17 @@ impl LifetimeName {
}
}

pub fn is_anonymous(&self) -> bool {
match *self {
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Implicit
| LifetimeName::Underscore
| LifetimeName::Param(ParamName::Fresh(_))
| LifetimeName::Error => true,
LifetimeName::Static | LifetimeName::Param(_) => false,
}
}

pub fn is_elided(&self) -> bool {
match self {
LifetimeName::ImplicitObjectLifetimeDefault
Expand Down
62 changes: 36 additions & 26 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -72,7 +72,7 @@ use rustc_middle::ty::{
subst::{GenericArgKind, Subst, SubstsRef},
Binder, EarlyBinder, List, Region, Ty, TyCtxt, TypeFoldable,
};
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
use rustc_span::{sym, symbol::kw, BytePos, DesugaringKind, Pos, Span};
use rustc_target::spec::abi;
use std::ops::ControlFlow;
use std::{cmp, fmt, iter};
Expand Down Expand Up @@ -161,35 +161,45 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
{
sp = param.span;
}
(format!("the lifetime `{}` as defined here", br.name), sp)
let text = if br.has_name() {
format!("the lifetime `{}` as defined here", br.name)
} else {
format!("the anonymous lifetime as defined here")
};
(text, sp)
}
ty::ReFree(ty::FreeRegion {
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
}) => {
let mut sp = sm.guess_head_span(tcx.def_span(scope));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
ty::ReFree(ref fr) => {
if !fr.bound_region.is_named()
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
{
sp = param.span;
}
(format!("the lifetime `{}` as defined here", name), sp)
}
ty::ReFree(ref fr) => match fr.bound_region {
ty::BrAnon(idx) => {
if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) {
("the anonymous lifetime defined here".to_string(), ty.span)
} else {
(
("the anonymous lifetime defined here".to_string(), ty.span)
} else {
match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => {
let mut sp = sm.guess_head_span(tcx.def_span(scope));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
sp = param.span;
}
let text = if name == kw::UnderscoreLifetime {
format!("the anonymous lifetime as defined here")
} else {
format!("the lifetime `{}` as defined here", name)
};
(text, sp)
}
ty::BrAnon(idx) => (
format!("the anonymous lifetime #{} defined here", idx + 1),
tcx.def_span(scope),
)
tcx.def_span(scope)
),
_ => (
format!("the lifetime `{}` as defined here", region),
sm.guess_head_span(tcx.def_span(scope)),
),
}
}
_ => (
format!("the lifetime `{}` as defined here", region),
sm.guess_head_span(tcx.def_span(scope)),
),
},
}
_ => bug!(),
}
}
Expand Down Expand Up @@ -2552,7 +2562,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
ty::ReEarlyBound(ty::EarlyBoundRegion { name, .. })
| ty::ReFree(ty::FreeRegion { bound_region: ty::BrNamed(_, name), .. }),
_,
) => {
) if name != kw::UnderscoreLifetime => {
// Does the required lifetime have a nice name we can print?
let mut err = struct_span_err!(
self.tcx.sess,
Expand Down
Expand Up @@ -12,6 +12,7 @@ use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::{GenericParamKind, Ty};
use rustc_middle::ty::Region;
use rustc_span::symbol::kw;

impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// Print the error message for lifetime errors when both the concerned regions are anonymous.
Expand Down Expand Up @@ -169,7 +170,7 @@ pub fn suggest_adding_lifetime_params<'tcx>(
return false;
};

if !lifetime_sub.name.is_elided() || !lifetime_sup.name.is_elided() {
if !lifetime_sub.name.is_anonymous() || !lifetime_sup.name.is_anonymous() {
return false;
};

Expand All @@ -188,32 +189,37 @@ pub fn suggest_adding_lifetime_params<'tcx>(
_ => return false,
};

let (suggestion_param_name, introduce_new) = generics
let suggestion_param_name = generics
.params
.iter()
.find(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }))
.and_then(|p| tcx.sess.source_map().span_to_snippet(p.span).ok())
.map(|name| (name, false))
.unwrap_or_else(|| ("'a".to_string(), true));

let mut suggestions = vec![
if let hir::LifetimeName::Underscore = lifetime_sub.name {
(lifetime_sub.span, suggestion_param_name.clone())
.filter(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }))
.map(|p| p.name.ident().name)
.find(|i| *i != kw::UnderscoreLifetime);
let introduce_new = suggestion_param_name.is_none();
let suggestion_param_name =
suggestion_param_name.map(|n| n.to_string()).unwrap_or_else(|| "'a".to_owned());

debug!(?lifetime_sup.span);
debug!(?lifetime_sub.span);
let make_suggestion = |span: rustc_span::Span| {
if span.is_empty() {
(span, format!("{}, ", suggestion_param_name))
} else if let Ok("&") = tcx.sess.source_map().span_to_snippet(span).as_deref() {
(span.shrink_to_hi(), format!("{} ", suggestion_param_name))
} else {
(lifetime_sub.span.shrink_to_hi(), suggestion_param_name.clone() + " ")
},
if let hir::LifetimeName::Underscore = lifetime_sup.name {
(lifetime_sup.span, suggestion_param_name.clone())
} else {
(lifetime_sup.span.shrink_to_hi(), suggestion_param_name.clone() + " ")
},
];
(span, suggestion_param_name.clone())
}
};
let mut suggestions =
vec![make_suggestion(lifetime_sub.span), make_suggestion(lifetime_sup.span)];

if introduce_new {
let new_param_suggestion = match &generics.params {
[] => (generics.span, format!("<{}>", suggestion_param_name)),
[first, ..] => (first.span.shrink_to_lo(), format!("{}, ", suggestion_param_name)),
};
let new_param_suggestion =
if let Some(first) = generics.params.iter().find(|p| !p.name.ident().span.is_empty()) {
(first.span.shrink_to_lo(), format!("{}, ", suggestion_param_name))
} else {
(generics.span, format!("<{}>", suggestion_param_name))
};

suggestions.push(new_param_suggestion);
}
Expand Down
Expand Up @@ -4,6 +4,7 @@ use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_middle::ty;
use rustc_span::symbol::kw;

impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// When given a `ConcreteFailure` for a function with parameters containing a named region and
Expand Down Expand Up @@ -67,7 +68,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let is_impl_item = region_info.is_impl_item;

match br {
ty::BrAnon(_) => {}
ty::BrNamed(_, kw::UnderscoreLifetime) | ty::BrAnon(_) => {}
_ => {
/* not an anonymous region */
debug!("try_report_named_anon_conflict: not an anonymous region");
Expand Down