Skip to content

Commit

Permalink
Auto merge of rust-lang#92397 - matthiaskrgr:rollup-xnfou17, r=matthi…
Browse files Browse the repository at this point in the history
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#92075 (rustdoc: Only special case struct fields for intra-doc links, not enum variants)
 - rust-lang#92118 (Parse and suggest moving where clauses after equals for type aliases)
 - rust-lang#92237 (Visit expressions in-order when resolving pattern bindings)
 - rust-lang#92340 (rustdoc: Start cleaning up search index generation)
 - rust-lang#92351 (Add long error explanation for E0227)
 - rust-lang#92371 (Remove pretty printer space inside block with only outer attrs)
 - rust-lang#92372 (Print space after formal generic params in fn type)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Dec 29, 2021
2 parents 6211dd7 + 949769c commit 2b67c30
Show file tree
Hide file tree
Showing 30 changed files with 382 additions and 130 deletions.
84 changes: 41 additions & 43 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,23 +387,23 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.print_string(sym.as_str(), style);
}

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

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

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

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

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

Expand All @@ -413,20 +413,21 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
kind: ast::AttrStyle,
is_inline: bool,
trailing_hardbreak: bool,
) {
let mut count = 0;
) -> bool {
let mut printed = false;
for attr in attrs {
if attr.style == kind {
self.print_attribute_inline(attr, is_inline);
if is_inline {
self.nbsp();
}
count += 1;
printed = true;
}
}
if count > 0 && trailing_hardbreak && !is_inline {
if printed && trailing_hardbreak && !is_inline {
self.hardbreak_if_not_bol();
}
printed
}

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

self.print_inner_attributes(attrs);
let has_attrs = self.print_inner_attributes(attrs);

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

let empty = attrs.is_empty() && blk.stmts.is_empty();
let empty = !has_attrs && blk.stmts.is_empty();
self.bclose_maybe_open(blk.span, empty, close_box);
self.ann.post(self, AnnNode::Block(blk))
}
Expand Down Expand Up @@ -2780,34 +2781,34 @@ impl<'a> State<'a> {
self.word_space(",");
}

match *predicate {
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
ref bound_generic_params,
ref bounded_ty,
ref bounds,
..
}) => {
self.print_formal_generic_params(bound_generic_params);
self.print_type(bounded_ty);
self.print_type_bounds(":", bounds);
}
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
ref lifetime,
ref bounds,
..
}) => {
self.print_lifetime_bounds(*lifetime, bounds);
}
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
ref lhs_ty,
ref rhs_ty,
..
}) => {
self.print_type(lhs_ty);
self.space();
self.word_space("=");
self.print_type(rhs_ty);
}
self.print_where_predicate(predicate);
}
}

pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) {
match predicate {
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
bound_generic_params,
bounded_ty,
bounds,
..
}) => {
self.print_formal_generic_params(bound_generic_params);
self.print_type(bounded_ty);
self.print_type_bounds(":", bounds);
}
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
lifetime,
bounds,
..
}) => {
self.print_lifetime_bounds(*lifetime, bounds);
}
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
self.print_type(lhs_ty);
self.space();
self.word_space("=");
self.print_type(rhs_ty);
}
}
}
Expand Down Expand Up @@ -2908,10 +2909,7 @@ impl<'a> State<'a> {
generic_params: &[ast::GenericParam],
) {
self.ibox(INDENT_UNIT);
if !generic_params.is_empty() {
self.word("for");
self.print_generic_params(generic_params);
}
self.print_formal_generic_params(generic_params);
let generics = ast::Generics {
params: Vec::new(),
where_clause: ast::WhereClause {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ E0223: include_str!("./error_codes/E0223.md"),
E0224: include_str!("./error_codes/E0224.md"),
E0225: include_str!("./error_codes/E0225.md"),
E0226: include_str!("./error_codes/E0226.md"),
E0227: include_str!("./error_codes/E0227.md"),
E0228: include_str!("./error_codes/E0228.md"),
E0229: include_str!("./error_codes/E0229.md"),
E0230: include_str!("./error_codes/E0230.md"),
Expand Down Expand Up @@ -530,7 +531,6 @@ E0786: include_str!("./error_codes/E0786.md"),
// E0217, // ambiguous associated type, defined in multiple supertraits
// E0218, // no associated type defined
// E0219, // associated type defined in higher-ranked supertrait
E0227, // ambiguous lifetime bound, explicit lifetime bound required
// E0233,
// E0234,
// E0235, // structure constructor specifies a structure of type but
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0227.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
This error indicates that the compiler is unable to determine whether there is
exactly one unique region in the set of derived region bounds.

Example of erroneous code:

```compile_fail,E0227
trait Foo<'foo>: 'foo {}
trait Bar<'bar>: 'bar {}
trait FooBar<'foo, 'bar>: Foo<'foo> + Bar<'bar> {}
struct Baz<'foo, 'bar> {
baz: dyn FooBar<'foo, 'bar>,
}
```

Here, `baz` can have either `'foo` or `'bar` lifetimes.

To resolve this error, provide an explicit lifetime:

```rust
trait Foo<'foo>: 'foo {}
trait Bar<'bar>: 'bar {}

trait FooBar<'foo, 'bar>: Foo<'foo> + Bar<'bar> {}

struct Baz<'foo, 'bar, 'baz>
where
'baz: 'foo + 'bar,
{
obj: dyn FooBar<'foo, 'bar> + 'baz,
}
```
5 changes: 1 addition & 4 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2327,10 +2327,7 @@ impl<'a> State<'a> {
arg_names: &[Ident],
) {
self.ibox(INDENT_UNIT);
if !generic_params.is_empty() {
self.word("for");
self.print_generic_params(generic_params);
}
self.print_formal_generic_params(generic_params);
let generics = hir::Generics {
params: &[],
where_clause: hir::WhereClause { predicates: &[], span: rustc_span::DUMMY_SP },
Expand Down
53 changes: 53 additions & 0 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,44 @@ impl<'a> Parser<'a> {
))
}

/// Emits an error that the where clause at the end of a type alias is not
/// allowed and suggests moving it.
fn error_ty_alias_where(
&self,
before_where_clause_present: bool,
before_where_clause_span: Span,
after_predicates: &[WherePredicate],
after_where_clause_span: Span,
) {
let mut err =
self.struct_span_err(after_where_clause_span, "where clause not allowed here");
if !after_predicates.is_empty() {
let mut state = crate::pprust::State::new();
if !before_where_clause_present {
state.space();
state.word_space("where");
} else {
state.word_space(",");
}
let mut first = true;
for p in after_predicates.iter() {
if !first {
state.word_space(",");
}
first = false;
state.print_where_predicate(p);
}
let suggestion = state.s.eof();
err.span_suggestion(
before_where_clause_span.shrink_to_hi(),
"move it here",
suggestion,
Applicability::MachineApplicable,
);
}
err.emit()
}

/// Parses a `type` alias with the following grammar:
/// ```
/// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
Expand All @@ -806,9 +844,24 @@ impl<'a> Parser<'a> {
// Parse optional colon and param bounds.
let bounds =
if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };

generics.where_clause = self.parse_where_clause()?;

let ty = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };

if self.token.is_keyword(kw::Where) {
let after_where_clause = self.parse_where_clause()?;

self.error_ty_alias_where(
generics.where_clause.has_where_token,
generics.where_clause.span,
&after_where_clause.predicates,
after_where_clause.span,
);

generics.where_clause.predicates.extend(after_where_clause.predicates.into_iter());
}

self.expect_semi()?;

Ok((ident, ItemKind::TyAlias(Box::new(TyAlias { defaultness, generics, bounds, ty }))))
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,10 +1603,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
pat_src: PatternSource,
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
) {
// We walk the pattern before declaring the pattern's inner bindings,
// so that we avoid resolving a literal expression to a binding defined
// by the pattern.
visit::walk_pat(self, pat);
self.resolve_pattern_inner(pat, pat_src, bindings);
// This has to happen *after* we determine which pat_idents are variants:
self.check_consistent_bindings_top(pat);
visit::walk_pat(self, pat);
}

/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
Expand Down
11 changes: 10 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use crate::clean::Clean;
use crate::core::DocContext;
use crate::formats::cache::Cache;
use crate::formats::item_type::ItemType;
use crate::html::render::cache::ExternalLocation;
use crate::html::render::Context;
use crate::passes::collect_intra_doc_links::UrlFragment;

Expand Down Expand Up @@ -339,6 +338,16 @@ impl ExternalCrate {
}
}

/// Indicates where an external crate can be found.
crate enum ExternalLocation {
/// Remote URL root of the external crate
Remote(String),
/// This external crate can be found in the local doc/ folder
Local,
/// The external crate could not be found.
Unknown,
}

/// Anything with a source location and set of attributes and, optionally, a
/// name. That is, anything that can be documented. This doesn't correspond
/// directly to the AST's concept of an item; it's a strict superset.
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;

use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::formats::item_type::ItemType;
use crate::formats::Impl;
use crate::html::markdown::short_markdown_summary;
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
use crate::html::render::search_index::get_function_type_for_search;
use crate::html::render::IndexItem;

/// This cache is used to store information about the [`clean::Crate`] being
Expand Down Expand Up @@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
desc,
parent,
parent_idx: None,
search_type: get_index_search_type(&item, self.tcx, self.cache),
search_type: get_function_type_for_search(&item, self.tcx),
aliases: item.attrs.get_doc_aliases(),
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::CRATE_DEF_INDEX;
use rustc_target::spec::abi::Abi;

use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, ItemId, PrimitiveType};
use crate::clean::{
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
PrimitiveType,
};
use crate::formats::item_type::ItemType;
use crate::html::escape::Escape;
use crate::html::render::cache::ExternalLocation;
use crate::html::render::Context;

use super::url_parts_builder::UrlPartsBuilder;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ use rustc_span::edition::Edition;
use rustc_span::source_map::FileName;
use rustc_span::symbol::sym;

use super::cache::{build_index, ExternalLocation};
use super::print_item::{full_path, item_path, print_item};
use super::search_index::build_index;
use super::templates;
use super::write_shared::write_shared;
use super::{
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
BASIC_KEYWORDS,
};

use crate::clean::{self, ExternalCrate};
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
use crate::config::RenderOptions;
use crate::docfs::{DocFS, PathError};
use crate::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! These threads are not parallelized (they haven't been a bottleneck yet), and
//! both occur before the crate is rendered.

crate mod cache;
crate mod search_index;

#[cfg(test)]
mod tests;
Expand Down
Loading

0 comments on commit 2b67c30

Please sign in to comment.