diff options
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state.rs | 93 | ||||
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state/item.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 21 | ||||
| -rw-r--r-- | compiler/rustc_trait_selection/src/lib.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/astconv/mod.rs | 4 |
7 files changed, 101 insertions, 54 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index b80a553b418..ad8dbfd506d 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -814,7 +814,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere } fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String { - Self::to_string(|s| s.print_type_bounds("", bounds)) + Self::to_string(|s| s.print_type_bounds(bounds)) } fn pat_to_string(&self, pat: &ast::Pat) -> String { @@ -991,7 +991,12 @@ impl<'a> State<'a> { Term::Const(c) => self.print_expr_anon_const(c, &[]), } } - ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds), + ast::AssocConstraintKind::Bound { bounds } => { + if !bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(&bounds); + } + } } } @@ -1045,11 +1050,14 @@ impl<'a> State<'a> { } ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false), ast::TyKind::TraitObject(ref bounds, syntax) => { - let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" }; - self.print_type_bounds(prefix, &bounds); + if syntax == ast::TraitObjectSyntax::Dyn { + self.word_nbsp("dyn"); + } + self.print_type_bounds(bounds); } ast::TyKind::ImplTrait(_, ref bounds) => { - self.print_type_bounds("impl", &bounds); + self.word_nbsp("impl"); + self.print_type_bounds(bounds); } ast::TyKind::Array(ref ty, ref length) => { self.word("["); @@ -1549,29 +1557,24 @@ impl<'a> State<'a> { } } - pub fn print_type_bounds(&mut self, prefix: &'static str, bounds: &[ast::GenericBound]) { - if !bounds.is_empty() { - self.word(prefix); - let mut first = true; - for bound in bounds { - if !(first && prefix.is_empty()) { - self.nbsp(); - } - if first { - first = false; - } else { - self.word_space("+"); - } + pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) { + let mut first = true; + for bound in bounds { + if first { + first = false; + } else { + self.nbsp(); + self.word_space("+"); + } - match bound { - GenericBound::Trait(tref, modifier) => { - if modifier == &TraitBoundModifier::Maybe { - self.word("?"); - } - self.print_poly_trait_ref(tref); + match bound { + GenericBound::Trait(tref, modifier) => { + if modifier == &TraitBoundModifier::Maybe { + self.word("?"); } - GenericBound::Outlives(lt) => self.print_lifetime(*lt), + self.print_poly_trait_ref(tref); } + GenericBound::Outlives(lt) => self.print_lifetime(*lt), } } } @@ -1580,22 +1583,14 @@ impl<'a> State<'a> { self.print_name(lifetime.ident.name) } - pub(crate) fn print_lifetime_bounds( - &mut self, - lifetime: ast::Lifetime, - bounds: &ast::GenericBounds, - ) { - self.print_lifetime(lifetime); - if !bounds.is_empty() { - self.word(": "); - for (i, bound) in bounds.iter().enumerate() { - if i != 0 { - self.word(" + "); - } - match bound { - ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt), - _ => panic!(), - } + pub(crate) fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) { + for (i, bound) in bounds.iter().enumerate() { + if i != 0 { + self.word(" + "); + } + match bound { + ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt), + _ => panic!(), } } } @@ -1613,11 +1608,18 @@ impl<'a> State<'a> { match param.kind { ast::GenericParamKind::Lifetime => { let lt = ast::Lifetime { id: param.id, ident: param.ident }; - s.print_lifetime_bounds(lt, ¶m.bounds) + s.print_lifetime(lt); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_lifetime_bounds(¶m.bounds) + } } ast::GenericParamKind::Type { ref default } => { s.print_ident(param.ident); - s.print_type_bounds(":", ¶m.bounds); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_type_bounds(¶m.bounds); + } if let Some(ref default) = default { s.space(); s.word_space("="); @@ -1630,7 +1632,10 @@ impl<'a> State<'a> { s.space(); s.word_space(":"); s.print_type(ty); - s.print_type_bounds(":", ¶m.bounds); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_type_bounds(¶m.bounds); + } if let Some(ref default) = default { s.space(); s.word_space("="); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 67b539a7ad4..f1caf22f364 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -114,7 +114,10 @@ impl<'a> State<'a> { self.word_space("type"); self.print_ident(ident); self.print_generic_params(&generics.params); - self.print_type_bounds(":", bounds); + if !bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(bounds); + } self.print_where_clause_parts(where_clauses.0.0, before_predicates); if let Some(ty) = ty { self.space(); @@ -320,7 +323,10 @@ impl<'a> State<'a> { real_bounds.push(b.clone()); } } - self.print_type_bounds(":", &real_bounds); + if !real_bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(&real_bounds); + } self.print_where_clause(&generics.where_clause); self.word(" "); self.bopen(); @@ -347,7 +353,10 @@ impl<'a> State<'a> { } } self.nbsp(); - self.print_type_bounds("=", &real_bounds); + if !real_bounds.is_empty() { + self.word_nbsp("="); + self.print_type_bounds(&real_bounds); + } self.print_where_clause(&generics.where_clause); self.word(";"); self.end(); // end inner head-block @@ -618,14 +627,23 @@ impl<'a> State<'a> { }) => { self.print_formal_generic_params(bound_generic_params); self.print_type(bounded_ty); - self.print_type_bounds(":", bounds); + self.word(":"); + if !bounds.is_empty() { + self.nbsp(); + self.print_type_bounds(bounds); + } } ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { lifetime, bounds, .. }) => { - self.print_lifetime_bounds(*lifetime, bounds); + self.print_lifetime(*lifetime); + self.word(":"); + if !bounds.is_empty() { + self.nbsp(); + self.print_lifetime_bounds(bounds); + } } ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => { self.print_type(lhs_ty); diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index acf892cec53..ba325d70422 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1355,7 +1355,10 @@ impl<'a> Parser<'a> { s.print_mutability(mut_ty.mutbl, false); s.popen(); s.print_type(&mut_ty.ty); - s.print_type_bounds(" +", &bounds); + if !bounds.is_empty() { + s.word(" + "); + s.print_type_bounds(&bounds); + } s.pclose() }); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e7c8886f054..86dbcba6c0d 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1914,6 +1914,8 @@ impl<'a> Resolver<'a> { }; } (msg, None) + } else if ident.name == kw::SelfUpper { + ("`Self` is only available in impls, traits, and type definitions".to_string(), None) } else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) { // Check whether the name refers to an item in the value namespace. let binding = if let Some(ribs) = ribs { diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 1a99bff610a..27bce60df2b 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -332,6 +332,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { span, "`Self` is only available in impls, traits, and type definitions".to_string(), ); + if let Some(item_kind) = self.diagnostic_metadata.current_item { + err.span_label( + item_kind.ident.span, + format!( + "`Self` not allowed in {} {}", + item_kind.kind.article(), + item_kind.kind.descr() + ), + ); + } return (err, Vec::new()); } if is_self_value(path, ns) { @@ -389,6 +399,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { ); } } + } else if let Some(item_kind) = self.diagnostic_metadata.current_item { + err.span_label( + item_kind.ident.span, + format!( + "`self` not allowed in {} {}", + item_kind.kind.article(), + item_kind.kind.descr() + ), + ); } return (err, Vec::new()); } @@ -1788,7 +1807,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { path: &[Segment], ) -> Option<(Span, &'static str, String, Applicability)> { let (ident, span) = match path { - [segment] if !segment.has_generic_args => { + [segment] if !segment.has_generic_args && segment.ident.name != kw::SelfUpper => { (segment.ident.to_string(), segment.ident.span) } _ => return None, diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index e4da6172916..44ff3fd7306 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -1,4 +1,4 @@ -//! This crates defines the trait resolution method. +//! This crate defines the trait resolution method. //! //! - **Traits.** Trait resolution is implemented in the `traits` module. //! diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 32bbfd7e332..4641b36aad1 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2195,8 +2195,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { "{kind} arguments are not allowed on {this_type}", ); err.span_label(last_span, format!("{kind} argument{s} not allowed")); - for (_, span) in types_and_spans { - err.span_label(span, "not allowed on this"); + for (what, span) in types_and_spans { + err.span_label(span, format!("not allowed on {what}")); } extend(&mut err); err.emit(); |
