Skip to content

Commit

Permalink
Auto merge of rust-lang#98284 - JohnTitor:rollup-7lbs143, r=JohnTitor
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - rust-lang#98183 (Fix pretty printing of empty bound lists in where-clause)
 - rust-lang#98268 (Improve `lifetime arguments are not allowed on` error message)
 - rust-lang#98273 (Fix minor documentation typo)
 - rust-lang#98274 (Minor improvements on error for `Self` type in items that don't allow it)
 - rust-lang#98281 (Fix typo in `HashMap::drain` docs)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 20, 2022
2 parents a5c039c + 66dbc3f commit 4104596
Show file tree
Hide file tree
Showing 35 changed files with 265 additions and 140 deletions.
93 changes: 49 additions & 44 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down Expand Up @@ -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("[");
Expand Down Expand Up @@ -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),
}
}
}
Expand All @@ -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!(),
}
}
}
Expand All @@ -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, &param.bounds)
s.print_lifetime(lt);
if !param.bounds.is_empty() {
s.word_nbsp(":");
s.print_lifetime_bounds(&param.bounds)
}
}
ast::GenericParamKind::Type { ref default } => {
s.print_ident(param.ident);
s.print_type_bounds(":", &param.bounds);
if !param.bounds.is_empty() {
s.word_nbsp(":");
s.print_type_bounds(&param.bounds);
}
if let Some(ref default) = default {
s.space();
s.word_space("=");
Expand All @@ -1630,7 +1632,10 @@ impl<'a> State<'a> {
s.space();
s.word_space(":");
s.print_type(ty);
s.print_type_bounds(":", &param.bounds);
if !param.bounds.is_empty() {
s.word_nbsp(":");
s.print_type_bounds(&param.bounds);
}
if let Some(ref default) = default {
s.space();
s.word_space("=");
Expand Down
28 changes: 23 additions & 5 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
//!
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl<K, V, S> HashMap<K, V, S> {
///
/// If the returned iterator is dropped before being fully consumed, it
/// drops the remaining key-value pairs. The returned iterator keeps a
/// mutable borrow on the vector to optimize its implementation.
/// mutable borrow on the map to optimize its implementation.
///
/// # Examples
///
Expand Down
3 changes: 3 additions & 0 deletions src/test/pretty/where-clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

fn f<'a, 'b, T>(t: T) -> isize where T: 'a, 'a: 'b, T: Eq { 0 }

// This is legal syntax, sometimes generated by macros. `where T: $($bound+)*`
fn zero_bounds<'a, T>() where 'a:, T: {}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/derives/issue-97343.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on type parameter `Irrelevant`
LL | #[derive(Debug)]
| -----
| |
| not allowed on this
| not allowed on type parameter `Irrelevant`
| in this derive macro expansion
LL | pub struct Irrelevant<Irrelevant> {
| ^^^^^^^^^^ type argument not allowed
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0109.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on this type
LL | type X = u32<i32>;
| --- ^^^ type argument not allowed
| |
| not allowed on this
| not allowed on this type
|
help: primitive type `u32` doesn't have generic parameters
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0110.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0109]: lifetime arguments are not allowed on this type
LL | type X = u32<'static>;
| --- ^^^^^^^ lifetime argument not allowed
| |
| not allowed on this
| not allowed on this type
|
help: primitive type `u32` doesn't have generic parameters
|
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/error-codes/E0411.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0411]: cannot find type `Self` in this scope
--> $DIR/E0411.rs:2:6
|
LL | fn main() {
| ---- `Self` not allowed in a function
LL | <Self>::foo;
| ^^^^ `Self` is only available in impls, traits, and type definitions

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-22706.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on module `marker`
LL | fn is_copy<T: ::std::marker<i32>::Copy>() {}
| ------ ^^^ type argument not allowed
| |
| not allowed on this
| not allowed on module `marker`

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-57924.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on self constructor
LL | Self::<E>(e)
| ---- ^ type argument not allowed
| |
| not allowed on this
| not allowed on self constructor

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-60989.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error[E0109]: type arguments are not allowed on local variable
LL | c1::<()>;
| -- ^^ type argument not allowed
| |
| not allowed on this
| not allowed on local variable

error[E0109]: type arguments are not allowed on local variable
--> $DIR/issue-60989.rs:16:10
|
LL | c1::<dyn Into<B>>;
| -- ^^^^^^^^^^^ type argument not allowed
| |
| not allowed on this
| not allowed on local variable

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lifetimes/issue-97194.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern "C" {
fn bget(&self, index: [usize; Self::DIM]) -> bool {
//~^ ERROR incorrect function inside `extern` block
//~| ERROR `self` parameter is only allowed in associated functions
//~| ERROR use of undeclared type `Self`
//~| ERROR failed to resolve: `Self`
type T<'a> = &'a str;
}
}
Expand Down
Loading

0 comments on commit 4104596

Please sign in to comment.