Skip to content

Commit 2b67c30

Browse files
committed
Auto merge of #92397 - matthiaskrgr:rollup-xnfou17, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #92075 (rustdoc: Only special case struct fields for intra-doc links, not enum variants) - #92118 (Parse and suggest moving where clauses after equals for type aliases) - #92237 (Visit expressions in-order when resolving pattern bindings) - #92340 (rustdoc: Start cleaning up search index generation) - #92351 (Add long error explanation for E0227) - #92371 (Remove pretty printer space inside block with only outer attrs) - #92372 (Print space after formal generic params in fn type) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6211dd7 + 949769c commit 2b67c30

30 files changed

+382
-130
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+41-43
Original file line numberDiff line numberDiff line change
@@ -387,23 +387,23 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
387387
self.print_string(sym.as_str(), style);
388388
}
389389

390-
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
390+
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
391391
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
392392
}
393393

394-
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) {
394+
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
395395
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
396396
}
397397

398-
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) {
398+
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
399399
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
400400
}
401401

402-
fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
402+
fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
403403
self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
404404
}
405405

406-
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
406+
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
407407
self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
408408
}
409409

@@ -413,20 +413,21 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
413413
kind: ast::AttrStyle,
414414
is_inline: bool,
415415
trailing_hardbreak: bool,
416-
) {
417-
let mut count = 0;
416+
) -> bool {
417+
let mut printed = false;
418418
for attr in attrs {
419419
if attr.style == kind {
420420
self.print_attribute_inline(attr, is_inline);
421421
if is_inline {
422422
self.nbsp();
423423
}
424-
count += 1;
424+
printed = true;
425425
}
426426
}
427-
if count > 0 && trailing_hardbreak && !is_inline {
427+
if printed && trailing_hardbreak && !is_inline {
428428
self.hardbreak_if_not_bol();
429429
}
430+
printed
430431
}
431432

432433
fn print_attribute(&mut self, attr: &ast::Attribute) {
@@ -1646,7 +1647,7 @@ impl<'a> State<'a> {
16461647
self.ann.pre(self, AnnNode::Block(blk));
16471648
self.bopen();
16481649

1649-
self.print_inner_attributes(attrs);
1650+
let has_attrs = self.print_inner_attributes(attrs);
16501651

16511652
for (i, st) in blk.stmts.iter().enumerate() {
16521653
match st.kind {
@@ -1660,7 +1661,7 @@ impl<'a> State<'a> {
16601661
}
16611662
}
16621663

1663-
let empty = attrs.is_empty() && blk.stmts.is_empty();
1664+
let empty = !has_attrs && blk.stmts.is_empty();
16641665
self.bclose_maybe_open(blk.span, empty, close_box);
16651666
self.ann.post(self, AnnNode::Block(blk))
16661667
}
@@ -2780,34 +2781,34 @@ impl<'a> State<'a> {
27802781
self.word_space(",");
27812782
}
27822783

2783-
match *predicate {
2784-
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
2785-
ref bound_generic_params,
2786-
ref bounded_ty,
2787-
ref bounds,
2788-
..
2789-
}) => {
2790-
self.print_formal_generic_params(bound_generic_params);
2791-
self.print_type(bounded_ty);
2792-
self.print_type_bounds(":", bounds);
2793-
}
2794-
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
2795-
ref lifetime,
2796-
ref bounds,
2797-
..
2798-
}) => {
2799-
self.print_lifetime_bounds(*lifetime, bounds);
2800-
}
2801-
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
2802-
ref lhs_ty,
2803-
ref rhs_ty,
2804-
..
2805-
}) => {
2806-
self.print_type(lhs_ty);
2807-
self.space();
2808-
self.word_space("=");
2809-
self.print_type(rhs_ty);
2810-
}
2784+
self.print_where_predicate(predicate);
2785+
}
2786+
}
2787+
2788+
pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
2789+
match predicate {
2790+
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
2791+
bound_generic_params,
2792+
bounded_ty,
2793+
bounds,
2794+
..
2795+
}) => {
2796+
self.print_formal_generic_params(bound_generic_params);
2797+
self.print_type(bounded_ty);
2798+
self.print_type_bounds(":", bounds);
2799+
}
2800+
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
2801+
lifetime,
2802+
bounds,
2803+
..
2804+
}) => {
2805+
self.print_lifetime_bounds(*lifetime, bounds);
2806+
}
2807+
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
2808+
self.print_type(lhs_ty);
2809+
self.space();
2810+
self.word_space("=");
2811+
self.print_type(rhs_ty);
28112812
}
28122813
}
28132814
}
@@ -2908,10 +2909,7 @@ impl<'a> State<'a> {
29082909
generic_params: &[ast::GenericParam],
29092910
) {
29102911
self.ibox(INDENT_UNIT);
2911-
if !generic_params.is_empty() {
2912-
self.word("for");
2913-
self.print_generic_params(generic_params);
2914-
}
2912+
self.print_formal_generic_params(generic_params);
29152913
let generics = ast::Generics {
29162914
params: Vec::new(),
29172915
where_clause: ast::WhereClause {

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ E0223: include_str!("./error_codes/E0223.md"),
120120
E0224: include_str!("./error_codes/E0224.md"),
121121
E0225: include_str!("./error_codes/E0225.md"),
122122
E0226: include_str!("./error_codes/E0226.md"),
123+
E0227: include_str!("./error_codes/E0227.md"),
123124
E0228: include_str!("./error_codes/E0228.md"),
124125
E0229: include_str!("./error_codes/E0229.md"),
125126
E0230: include_str!("./error_codes/E0230.md"),
@@ -530,7 +531,6 @@ E0786: include_str!("./error_codes/E0786.md"),
530531
// E0217, // ambiguous associated type, defined in multiple supertraits
531532
// E0218, // no associated type defined
532533
// E0219, // associated type defined in higher-ranked supertrait
533-
E0227, // ambiguous lifetime bound, explicit lifetime bound required
534534
// E0233,
535535
// E0234,
536536
// E0235, // structure constructor specifies a structure of type but
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
This error indicates that the compiler is unable to determine whether there is
2+
exactly one unique region in the set of derived region bounds.
3+
4+
Example of erroneous code:
5+
6+
```compile_fail,E0227
7+
trait Foo<'foo>: 'foo {}
8+
trait Bar<'bar>: 'bar {}
9+
10+
trait FooBar<'foo, 'bar>: Foo<'foo> + Bar<'bar> {}
11+
12+
struct Baz<'foo, 'bar> {
13+
baz: dyn FooBar<'foo, 'bar>,
14+
}
15+
```
16+
17+
Here, `baz` can have either `'foo` or `'bar` lifetimes.
18+
19+
To resolve this error, provide an explicit lifetime:
20+
21+
```rust
22+
trait Foo<'foo>: 'foo {}
23+
trait Bar<'bar>: 'bar {}
24+
25+
trait FooBar<'foo, 'bar>: Foo<'foo> + Bar<'bar> {}
26+
27+
struct Baz<'foo, 'bar, 'baz>
28+
where
29+
'baz: 'foo + 'bar,
30+
{
31+
obj: dyn FooBar<'foo, 'bar> + 'baz,
32+
}
33+
```

compiler/rustc_hir_pretty/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2327,10 +2327,7 @@ impl<'a> State<'a> {
23272327
arg_names: &[Ident],
23282328
) {
23292329
self.ibox(INDENT_UNIT);
2330-
if !generic_params.is_empty() {
2331-
self.word("for");
2332-
self.print_generic_params(generic_params);
2333-
}
2330+
self.print_formal_generic_params(generic_params);
23342331
let generics = hir::Generics {
23352332
params: &[],
23362333
where_clause: hir::WhereClause { predicates: &[], span: rustc_span::DUMMY_SP },

compiler/rustc_parse/src/parser/item.rs

+53
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,44 @@ impl<'a> Parser<'a> {
794794
))
795795
}
796796

797+
/// Emits an error that the where clause at the end of a type alias is not
798+
/// allowed and suggests moving it.
799+
fn error_ty_alias_where(
800+
&self,
801+
before_where_clause_present: bool,
802+
before_where_clause_span: Span,
803+
after_predicates: &[WherePredicate],
804+
after_where_clause_span: Span,
805+
) {
806+
let mut err =
807+
self.struct_span_err(after_where_clause_span, "where clause not allowed here");
808+
if !after_predicates.is_empty() {
809+
let mut state = crate::pprust::State::new();
810+
if !before_where_clause_present {
811+
state.space();
812+
state.word_space("where");
813+
} else {
814+
state.word_space(",");
815+
}
816+
let mut first = true;
817+
for p in after_predicates.iter() {
818+
if !first {
819+
state.word_space(",");
820+
}
821+
first = false;
822+
state.print_where_predicate(p);
823+
}
824+
let suggestion = state.s.eof();
825+
err.span_suggestion(
826+
before_where_clause_span.shrink_to_hi(),
827+
"move it here",
828+
suggestion,
829+
Applicability::MachineApplicable,
830+
);
831+
}
832+
err.emit()
833+
}
834+
797835
/// Parses a `type` alias with the following grammar:
798836
/// ```
799837
/// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
@@ -806,9 +844,24 @@ impl<'a> Parser<'a> {
806844
// Parse optional colon and param bounds.
807845
let bounds =
808846
if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
847+
809848
generics.where_clause = self.parse_where_clause()?;
810849

811850
let ty = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
851+
852+
if self.token.is_keyword(kw::Where) {
853+
let after_where_clause = self.parse_where_clause()?;
854+
855+
self.error_ty_alias_where(
856+
generics.where_clause.has_where_token,
857+
generics.where_clause.span,
858+
&after_where_clause.predicates,
859+
after_where_clause.span,
860+
);
861+
862+
generics.where_clause.predicates.extend(after_where_clause.predicates.into_iter());
863+
}
864+
812865
self.expect_semi()?;
813866

814867
Ok((ident, ItemKind::TyAlias(Box::new(TyAlias { defaultness, generics, bounds, ty }))))

compiler/rustc_resolve/src/late.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1603,10 +1603,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16031603
pat_src: PatternSource,
16041604
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
16051605
) {
1606+
// We walk the pattern before declaring the pattern's inner bindings,
1607+
// so that we avoid resolving a literal expression to a binding defined
1608+
// by the pattern.
1609+
visit::walk_pat(self, pat);
16061610
self.resolve_pattern_inner(pat, pat_src, bindings);
16071611
// This has to happen *after* we determine which pat_idents are variants:
16081612
self.check_consistent_bindings_top(pat);
1609-
visit::walk_pat(self, pat);
16101613
}
16111614

16121615
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.

src/librustdoc/clean/types.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use crate::clean::Clean;
3939
use crate::core::DocContext;
4040
use crate::formats::cache::Cache;
4141
use crate::formats::item_type::ItemType;
42-
use crate::html::render::cache::ExternalLocation;
4342
use crate::html::render::Context;
4443
use crate::passes::collect_intra_doc_links::UrlFragment;
4544

@@ -339,6 +338,16 @@ impl ExternalCrate {
339338
}
340339
}
341340

341+
/// Indicates where an external crate can be found.
342+
crate enum ExternalLocation {
343+
/// Remote URL root of the external crate
344+
Remote(String),
345+
/// This external crate can be found in the local doc/ folder
346+
Local,
347+
/// The external crate could not be found.
348+
Unknown,
349+
}
350+
342351
/// Anything with a source location and set of attributes and, optionally, a
343352
/// name. That is, anything that can be documented. This doesn't correspond
344353
/// directly to the AST's concept of an item; it's a strict superset.

src/librustdoc/formats/cache.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88

9-
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
9+
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
1010
use crate::core::DocContext;
1111
use crate::fold::DocFolder;
1212
use crate::formats::item_type::ItemType;
1313
use crate::formats::Impl;
1414
use crate::html::markdown::short_markdown_summary;
15-
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
15+
use crate::html::render::search_index::get_function_type_for_search;
1616
use crate::html::render::IndexItem;
1717

1818
/// This cache is used to store information about the [`clean::Crate`] being
@@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
303303
desc,
304304
parent,
305305
parent_idx: None,
306-
search_type: get_index_search_type(&item, self.tcx, self.cache),
306+
search_type: get_function_type_for_search(&item, self.tcx),
307307
aliases: item.attrs.get_doc_aliases(),
308308
});
309309
}

src/librustdoc/html/format.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ use rustc_middle::ty::TyCtxt;
2121
use rustc_span::def_id::CRATE_DEF_INDEX;
2222
use rustc_target::spec::abi::Abi;
2323

24-
use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, ItemId, PrimitiveType};
24+
use crate::clean::{
25+
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
26+
PrimitiveType,
27+
};
2528
use crate::formats::item_type::ItemType;
2629
use crate::html::escape::Escape;
27-
use crate::html::render::cache::ExternalLocation;
2830
use crate::html::render::Context;
2931

3032
use super::url_parts_builder::UrlPartsBuilder;

src/librustdoc/html/render/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ use rustc_span::edition::Edition;
1313
use rustc_span::source_map::FileName;
1414
use rustc_span::symbol::sym;
1515

16-
use super::cache::{build_index, ExternalLocation};
1716
use super::print_item::{full_path, item_path, print_item};
17+
use super::search_index::build_index;
1818
use super::templates;
1919
use super::write_shared::write_shared;
2020
use super::{
2121
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
2222
BASIC_KEYWORDS,
2323
};
2424

25-
use crate::clean::{self, ExternalCrate};
25+
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
2626
use crate::config::RenderOptions;
2727
use crate::docfs::{DocFS, PathError};
2828
use crate::error::Error;

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! These threads are not parallelized (they haven't been a bottleneck yet), and
2424
//! both occur before the crate is rendered.
2525
26-
crate mod cache;
26+
crate mod search_index;
2727

2828
#[cfg(test)]
2929
mod tests;

0 commit comments

Comments
 (0)