Rework diagnostics for wrong number of generic args

This commit is contained in:
Patryk Wychowaniec 2021-01-02 19:45:11 +01:00
parent 34628e5b53
commit d2f8e398f1
No known key found for this signature in database
GPG key ID: F62547D075E09767
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