Auto merge of #77524 - Patryk27:fixes/66228, r=estebank

Rework diagnostics for wrong number of generic args (fixes #66228 and #71924)

This PR reworks the `wrong number of {} arguments` message, so that it provides more details and contextual hints.
This commit is contained in:
bors 2021-01-13 20:35:58 +00:00
commit a62a76047e
121 changed files with 2787 additions and 766 deletions

View file

@ -11,9 +11,9 @@ pub use rustc_ast::{CaptureBy, Movability, Mutability};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
use rustc_macros::HashStable_Generic;
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Spanned;
use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{def_id::LocalDefId, BytePos};
use rustc_span::{MultiSpan, Span, DUMMY_SP};
use rustc_target::asm::InlineAsmRegOrRegClass;
use rustc_target::spec::abi::Abi;
@ -231,7 +231,11 @@ impl<'hir> PathSegment<'hir> {
PathSegment { ident, hir_id: None, res: None, infer_args: true, args: None }
}
pub fn generic_args(&self) -> &GenericArgs<'hir> {
pub fn invalid() -> Self {
Self::from_ident(Ident::invalid())
}
pub fn args(&self) -> &GenericArgs<'hir> {
if let Some(ref args) = self.args {
args
} else {
@ -275,6 +279,10 @@ impl GenericArg<'_> {
matches!(self, GenericArg::Const(_))
}
pub fn is_synthetic(&self) -> bool {
matches!(self, GenericArg::Lifetime(lifetime) if lifetime.name.ident() == Ident::invalid())
}
pub fn descr(&self) -> &'static str {
match self {
GenericArg::Lifetime(_) => "lifetime",
@ -283,14 +291,6 @@ impl GenericArg<'_> {
}
}
pub fn short_descr(&self) -> &'static str {
match self {
GenericArg::Lifetime(_) => "lifetime",
GenericArg::Type(_) => "type",
GenericArg::Const(_) => "const",
}
}
pub fn to_ord(&self, feats: &rustc_feature::Features) -> ast::ParamKindOrd {
match self {
GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
@ -352,6 +352,39 @@ impl GenericArgs<'_> {
own_counts
}
pub fn span(&self) -> Option<Span> {
self.args
.iter()
.filter(|arg| !arg.is_synthetic())
.map(|arg| arg.span())
.fold_first(|span1, span2| span1.to(span2))
}
/// Returns span encompassing arguments and their surrounding `<>` or `()`
pub fn span_ext(&self, sm: &SourceMap) -> Option<Span> {
let mut span = self.span()?;
let (o, c) = if self.parenthesized { ('(', ')') } else { ('<', '>') };
if let Ok(snippet) = sm.span_to_snippet(span) {
let snippet = snippet.as_bytes();
if snippet[0] != (o as u8) || snippet[snippet.len() - 1] != (c as u8) {
span = sm.span_extend_to_prev_char(span, o, true);
span = span.with_lo(span.lo() - BytePos(1));
span = sm.span_extend_to_next_char(span, c, true);
span = span.with_hi(span.hi() + BytePos(1));
}
}
Some(span)
}
pub fn is_empty(&self) -> bool {
self.args.is_empty()
}
}
/// A modifier on a bound, currently this is only used for `?Sized`, where the

View file

@ -157,7 +157,7 @@ where
}
language_item_table! {
// Variant name, Name, Method name, Target;
// Variant name, Name, Method name, Target;
Bool, sym::bool, bool_impl, Target::Impl;
Char, sym::char, char_impl, Target::Impl;
Str, sym::str, str_impl, Target::Impl;

View file

@ -7,6 +7,7 @@
#![feature(const_fn)] // For the unsizing cast on `&[]`
#![feature(const_panic)]
#![feature(in_band_lifetimes)]
#![feature(iterator_fold_self)]
#![feature(once_cell)]
#![feature(or_patterns)]
#![recursion_limit = "256"]