Skip to content

Commit 4104596

Browse files
committedJun 20, 2022
Auto merge of rust-lang#98284 - JohnTitor:rollup-7lbs143, r=JohnTitor
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
2 parents a5c039c + 66dbc3f commit 4104596

35 files changed

+265
-140
lines changed
 

‎compiler/rustc_ast_pretty/src/pprust/state.rs

+49-44
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
814814
}
815815

816816
fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String {
817-
Self::to_string(|s| s.print_type_bounds("", bounds))
817+
Self::to_string(|s| s.print_type_bounds(bounds))
818818
}
819819

820820
fn pat_to_string(&self, pat: &ast::Pat) -> String {
@@ -991,7 +991,12 @@ impl<'a> State<'a> {
991991
Term::Const(c) => self.print_expr_anon_const(c, &[]),
992992
}
993993
}
994-
ast::AssocConstraintKind::Bound { bounds } => self.print_type_bounds(":", &*bounds),
994+
ast::AssocConstraintKind::Bound { bounds } => {
995+
if !bounds.is_empty() {
996+
self.word_nbsp(":");
997+
self.print_type_bounds(&bounds);
998+
}
999+
}
9951000
}
9961001
}
9971002

@@ -1045,11 +1050,14 @@ impl<'a> State<'a> {
10451050
}
10461051
ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false),
10471052
ast::TyKind::TraitObject(ref bounds, syntax) => {
1048-
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
1049-
self.print_type_bounds(prefix, &bounds);
1053+
if syntax == ast::TraitObjectSyntax::Dyn {
1054+
self.word_nbsp("dyn");
1055+
}
1056+
self.print_type_bounds(bounds);
10501057
}
10511058
ast::TyKind::ImplTrait(_, ref bounds) => {
1052-
self.print_type_bounds("impl", &bounds);
1059+
self.word_nbsp("impl");
1060+
self.print_type_bounds(bounds);
10531061
}
10541062
ast::TyKind::Array(ref ty, ref length) => {
10551063
self.word("[");
@@ -1549,29 +1557,24 @@ impl<'a> State<'a> {
15491557
}
15501558
}
15511559

1552-
pub fn print_type_bounds(&mut self, prefix: &'static str, bounds: &[ast::GenericBound]) {
1553-
if !bounds.is_empty() {
1554-
self.word(prefix);
1555-
let mut first = true;
1556-
for bound in bounds {
1557-
if !(first && prefix.is_empty()) {
1558-
self.nbsp();
1559-
}
1560-
if first {
1561-
first = false;
1562-
} else {
1563-
self.word_space("+");
1564-
}
1560+
pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) {
1561+
let mut first = true;
1562+
for bound in bounds {
1563+
if first {
1564+
first = false;
1565+
} else {
1566+
self.nbsp();
1567+
self.word_space("+");
1568+
}
15651569

1566-
match bound {
1567-
GenericBound::Trait(tref, modifier) => {
1568-
if modifier == &TraitBoundModifier::Maybe {
1569-
self.word("?");
1570-
}
1571-
self.print_poly_trait_ref(tref);
1570+
match bound {
1571+
GenericBound::Trait(tref, modifier) => {
1572+
if modifier == &TraitBoundModifier::Maybe {
1573+
self.word("?");
15721574
}
1573-
GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1575+
self.print_poly_trait_ref(tref);
15741576
}
1577+
GenericBound::Outlives(lt) => self.print_lifetime(*lt),
15751578
}
15761579
}
15771580
}
@@ -1580,22 +1583,14 @@ impl<'a> State<'a> {
15801583
self.print_name(lifetime.ident.name)
15811584
}
15821585

1583-
pub(crate) fn print_lifetime_bounds(
1584-
&mut self,
1585-
lifetime: ast::Lifetime,
1586-
bounds: &ast::GenericBounds,
1587-
) {
1588-
self.print_lifetime(lifetime);
1589-
if !bounds.is_empty() {
1590-
self.word(": ");
1591-
for (i, bound) in bounds.iter().enumerate() {
1592-
if i != 0 {
1593-
self.word(" + ");
1594-
}
1595-
match bound {
1596-
ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1597-
_ => panic!(),
1598-
}
1586+
pub(crate) fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) {
1587+
for (i, bound) in bounds.iter().enumerate() {
1588+
if i != 0 {
1589+
self.word(" + ");
1590+
}
1591+
match bound {
1592+
ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt),
1593+
_ => panic!(),
15991594
}
16001595
}
16011596
}
@@ -1613,11 +1608,18 @@ impl<'a> State<'a> {
16131608
match param.kind {
16141609
ast::GenericParamKind::Lifetime => {
16151610
let lt = ast::Lifetime { id: param.id, ident: param.ident };
1616-
s.print_lifetime_bounds(lt, &param.bounds)
1611+
s.print_lifetime(lt);
1612+
if !param.bounds.is_empty() {
1613+
s.word_nbsp(":");
1614+
s.print_lifetime_bounds(&param.bounds)
1615+
}
16171616
}
16181617
ast::GenericParamKind::Type { ref default } => {
16191618
s.print_ident(param.ident);
1620-
s.print_type_bounds(":", &param.bounds);
1619+
if !param.bounds.is_empty() {
1620+
s.word_nbsp(":");
1621+
s.print_type_bounds(&param.bounds);
1622+
}
16211623
if let Some(ref default) = default {
16221624
s.space();
16231625
s.word_space("=");
@@ -1630,7 +1632,10 @@ impl<'a> State<'a> {
16301632
s.space();
16311633
s.word_space(":");
16321634
s.print_type(ty);
1633-
s.print_type_bounds(":", &param.bounds);
1635+
if !param.bounds.is_empty() {
1636+
s.word_nbsp(":");
1637+
s.print_type_bounds(&param.bounds);
1638+
}
16341639
if let Some(ref default) = default {
16351640
s.space();
16361641
s.word_space("=");

‎compiler/rustc_ast_pretty/src/pprust/state/item.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ impl<'a> State<'a> {
114114
self.word_space("type");
115115
self.print_ident(ident);
116116
self.print_generic_params(&generics.params);
117-
self.print_type_bounds(":", bounds);
117+
if !bounds.is_empty() {
118+
self.word_nbsp(":");
119+
self.print_type_bounds(bounds);
120+
}
118121
self.print_where_clause_parts(where_clauses.0.0, before_predicates);
119122
if let Some(ty) = ty {
120123
self.space();
@@ -320,7 +323,10 @@ impl<'a> State<'a> {
320323
real_bounds.push(b.clone());
321324
}
322325
}
323-
self.print_type_bounds(":", &real_bounds);
326+
if !real_bounds.is_empty() {
327+
self.word_nbsp(":");
328+
self.print_type_bounds(&real_bounds);
329+
}
324330
self.print_where_clause(&generics.where_clause);
325331
self.word(" ");
326332
self.bopen();
@@ -347,7 +353,10 @@ impl<'a> State<'a> {
347353
}
348354
}
349355
self.nbsp();
350-
self.print_type_bounds("=", &real_bounds);
356+
if !real_bounds.is_empty() {
357+
self.word_nbsp("=");
358+
self.print_type_bounds(&real_bounds);
359+
}
351360
self.print_where_clause(&generics.where_clause);
352361
self.word(";");
353362
self.end(); // end inner head-block
@@ -618,14 +627,23 @@ impl<'a> State<'a> {
618627
}) => {
619628
self.print_formal_generic_params(bound_generic_params);
620629
self.print_type(bounded_ty);
621-
self.print_type_bounds(":", bounds);
630+
self.word(":");
631+
if !bounds.is_empty() {
632+
self.nbsp();
633+
self.print_type_bounds(bounds);
634+
}
622635
}
623636
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
624637
lifetime,
625638
bounds,
626639
..
627640
}) => {
628-
self.print_lifetime_bounds(*lifetime, bounds);
641+
self.print_lifetime(*lifetime);
642+
self.word(":");
643+
if !bounds.is_empty() {
644+
self.nbsp();
645+
self.print_lifetime_bounds(bounds);
646+
}
629647
}
630648
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
631649
self.print_type(lhs_ty);

‎compiler/rustc_parse/src/parser/diagnostics.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,10 @@ impl<'a> Parser<'a> {
13551355
s.print_mutability(mut_ty.mutbl, false);
13561356
s.popen();
13571357
s.print_type(&mut_ty.ty);
1358-
s.print_type_bounds(" +", &bounds);
1358+
if !bounds.is_empty() {
1359+
s.word(" + ");
1360+
s.print_type_bounds(&bounds);
1361+
}
13591362
s.pclose()
13601363
});
13611364

‎compiler/rustc_resolve/src/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,8 @@ impl<'a> Resolver<'a> {
19141914
};
19151915
}
19161916
(msg, None)
1917+
} else if ident.name == kw::SelfUpper {
1918+
("`Self` is only available in impls, traits, and type definitions".to_string(), None)
19171919
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
19181920
// Check whether the name refers to an item in the value namespace.
19191921
let binding = if let Some(ribs) = ribs {

‎compiler/rustc_resolve/src/late/diagnostics.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
332332
span,
333333
"`Self` is only available in impls, traits, and type definitions".to_string(),
334334
);
335+
if let Some(item_kind) = self.diagnostic_metadata.current_item {
336+
err.span_label(
337+
item_kind.ident.span,
338+
format!(
339+
"`Self` not allowed in {} {}",
340+
item_kind.kind.article(),
341+
item_kind.kind.descr()
342+
),
343+
);
344+
}
335345
return (err, Vec::new());
336346
}
337347
if is_self_value(path, ns) {
@@ -389,6 +399,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
389399
);
390400
}
391401
}
402+
} else if let Some(item_kind) = self.diagnostic_metadata.current_item {
403+
err.span_label(
404+
item_kind.ident.span,
405+
format!(
406+
"`self` not allowed in {} {}",
407+
item_kind.kind.article(),
408+
item_kind.kind.descr()
409+
),
410+
);
392411
}
393412
return (err, Vec::new());
394413
}
@@ -1788,7 +1807,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
17881807
path: &[Segment],
17891808
) -> Option<(Span, &'static str, String, Applicability)> {
17901809
let (ident, span) = match path {
1791-
[segment] if !segment.has_generic_args => {
1810+
[segment] if !segment.has_generic_args && segment.ident.name != kw::SelfUpper => {
17921811
(segment.ident.to_string(), segment.ident.span)
17931812
}
17941813
_ => return None,

‎compiler/rustc_trait_selection/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! This crates defines the trait resolution method.
1+
//! This crate defines the trait resolution method.
22
//!
33
//! - **Traits.** Trait resolution is implemented in the `traits` module.
44
//!

‎compiler/rustc_typeck/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2195,8 +2195,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21952195
"{kind} arguments are not allowed on {this_type}",
21962196
);
21972197
err.span_label(last_span, format!("{kind} argument{s} not allowed"));
2198-
for (_, span) in types_and_spans {
2199-
err.span_label(span, "not allowed on this");
2198+
for (what, span) in types_and_spans {
2199+
err.span_label(span, format!("not allowed on {what}"));
22002200
}
22012201
extend(&mut err);
22022202
err.emit();

‎library/std/src/collections/hash/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<K, V, S> HashMap<K, V, S> {
588588
///
589589
/// If the returned iterator is dropped before being fully consumed, it
590590
/// drops the remaining key-value pairs. The returned iterator keeps a
591-
/// mutable borrow on the vector to optimize its implementation.
591+
/// mutable borrow on the map to optimize its implementation.
592592
///
593593
/// # Examples
594594
///

‎src/test/pretty/where-clauses.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

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

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

‎src/test/ui/derives/issue-97343.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on type parameter `Irrelevant`
44
LL | #[derive(Debug)]
55
| -----
66
| |
7-
| not allowed on this
7+
| not allowed on type parameter `Irrelevant`
88
| in this derive macro expansion
99
LL | pub struct Irrelevant<Irrelevant> {
1010
| ^^^^^^^^^^ type argument not allowed

‎src/test/ui/error-codes/E0109.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on this type
44
LL | type X = u32<i32>;
55
| --- ^^^ type argument not allowed
66
| |
7-
| not allowed on this
7+
| not allowed on this type
88
|
99
help: primitive type `u32` doesn't have generic parameters
1010
|

‎src/test/ui/error-codes/E0110.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0109]: lifetime arguments are not allowed on this type
44
LL | type X = u32<'static>;
55
| --- ^^^^^^^ lifetime argument not allowed
66
| |
7-
| not allowed on this
7+
| not allowed on this type
88
|
99
help: primitive type `u32` doesn't have generic parameters
1010
|

‎src/test/ui/error-codes/E0411.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0411]: cannot find type `Self` in this scope
22
--> $DIR/E0411.rs:2:6
33
|
4+
LL | fn main() {
5+
| ---- `Self` not allowed in a function
46
LL | <Self>::foo;
57
| ^^^^ `Self` is only available in impls, traits, and type definitions
68

‎src/test/ui/issues/issue-22706.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on module `marker`
44
LL | fn is_copy<T: ::std::marker<i32>::Copy>() {}
55
| ------ ^^^ type argument not allowed
66
| |
7-
| not allowed on this
7+
| not allowed on module `marker`
88

99
error: aborting due to previous error
1010

‎src/test/ui/issues/issue-57924.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0109]: type arguments are not allowed on self constructor
44
LL | Self::<E>(e)
55
| ---- ^ type argument not allowed
66
| |
7-
| not allowed on this
7+
| not allowed on self constructor
88

99
error: aborting due to previous error
1010

‎src/test/ui/issues/issue-60989.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error[E0109]: type arguments are not allowed on local variable
44
LL | c1::<()>;
55
| -- ^^ type argument not allowed
66
| |
7-
| not allowed on this
7+
| not allowed on local variable
88

99
error[E0109]: type arguments are not allowed on local variable
1010
--> $DIR/issue-60989.rs:16:10
1111
|
1212
LL | c1::<dyn Into<B>>;
1313
| -- ^^^^^^^^^^^ type argument not allowed
1414
| |
15-
| not allowed on this
15+
| not allowed on local variable
1616

1717
error: aborting due to 2 previous errors
1818

‎src/test/ui/lifetimes/issue-97194.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern "C" {
22
fn bget(&self, index: [usize; Self::DIM]) -> bool {
33
//~^ ERROR incorrect function inside `extern` block
44
//~| ERROR `self` parameter is only allowed in associated functions
5-
//~| ERROR use of undeclared type `Self`
5+
//~| ERROR failed to resolve: `Self`
66
type T<'a> = &'a str;
77
}
88
}

0 commit comments

Comments
 (0)
Please sign in to comment.