Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #107584

Merged
merged 10 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,30 @@ pub fn expand_deriving_partial_eq(
cx.span_bug(field.span, "not exactly 2 arguments in `derive(PartialEq)`");
};

// We received `&T` arguments. Convert them to `T` by
// stripping `&` or adding `*`. This isn't necessary for
// type checking, but it results in much better error
// messages if something goes wrong.
// We received arguments of type `&T`. Convert them to type `T` by stripping
// any leading `&` or adding `*`. This isn't necessary for type checking, but
// it results in better error messages if something goes wrong.
//
// Note: for arguments that look like `&{ x }`, which occur with packed
// structs, this would cause expressions like `{ self.x } == { other.x }`,
// which isn't valid Rust syntax. This wouldn't break compilation because these
// AST nodes are constructed within the compiler. But it would mean that code
// printed by `-Zunpretty=expanded` (or `cargo expand`) would have invalid
// syntax, which would be suboptimal. So we wrap these in parens, giving
// `({ self.x }) == ({ other.x })`, which is valid syntax.
let convert = |expr: &P<Expr>| {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) =
&expr.kind
{
inner.clone()
if let ExprKind::Block(..) = &inner.kind {
// `&{ x }` form: remove the `&`, add parens.
cx.expr_paren(field.span, inner.clone())
} else {
// `&x` form: remove the `&`.
inner.clone()
}
} else {
// No leading `&`: add a leading `*`.
cx.expr_deref(field.span, expr.clone())
}
};
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ impl<'a> ExtCtxt<'a> {
self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
}

pub fn expr_paren(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
self.expr(sp, ast::ExprKind::Paren(e))
}

pub fn expr_call(
&self,
span: Span,
Expand Down
62 changes: 16 additions & 46 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use crate::traits::{

use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString, MultiSpan};
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -1470,51 +1470,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
for (key, values) in types.iter() {
let count = values.len();
let kind = key.descr();
let mut returned_async_output_error = false;
for &sp in values {
if sp.is_desugaring(DesugaringKind::Async) && !returned_async_output_error {
if [sp] != err.span.primary_spans() {
let mut span: MultiSpan = sp.into();
span.push_span_label(
sp,
format!(
"checked the `Output` of this `async fn`, {}{} {}{}",
if count > 1 { "one of the " } else { "" },
target,
kind,
pluralize!(count),
),
);
err.span_note(
span,
"while checking the return type of the `async fn`",
);
} else {
err.span_label(
sp,
format!(
"checked the `Output` of this `async fn`, {}{} {}{}",
if count > 1 { "one of the " } else { "" },
target,
kind,
pluralize!(count),
),
);
err.note("while checking the return type of the `async fn`");
}
returned_async_output_error = true;
} else {
err.span_label(
sp,
format!(
"{}{} {}{}",
if count == 1 { "the " } else { "one of the " },
target,
kind,
pluralize!(count),
),
);
}
err.span_label(
sp,
format!(
"{}{} {}{}",
if count == 1 { "the " } else { "one of the " },
target,
kind,
pluralize!(count),
),
);
}
}
}
Expand All @@ -1537,7 +1503,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// |
// = note: expected unit type `()`
// found closure `[closure@$DIR/issue-20862.rs:2:5: 2:14 x:_]`
if !self.ignore_span.overlaps(span) {
//
// Also ignore opaque `Future`s that come from async fns.
if !self.ignore_span.overlaps(span)
&& !span.is_desugaring(DesugaringKind::Async)
{
self.types.entry(kind).or_default().insert(span);
}
}
Expand Down
78 changes: 78 additions & 0 deletions src/doc/style-guide/src/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,84 @@ let Foo {
);
```

#### else blocks (let-else statements)

If a let statement contains an `else` component, also known as a let-else statement,
then the `else` component should be formatted according to the same rules as the `else` block
in [control flow expressions (i.e. if-else, and if-let-else expressions)](./expressions.md#control-flow-expressions).
Apply the same formatting rules to the components preceding
the `else` block (i.e. the `let pattern: Type = initializer_expr ...` portion)
as described [above](#let-statements)

Similarly to if-else expressions, if the initializer
expression is multi-lined, then the `else` keyword and opening brace of the block (i.e. `else {`)
should be put on the same line as the end of the initializer
expression with a preceding space if all the following are true:

* The initializer expression ends with one or more closing
parentheses, square brackets, and/or braces
* There is nothing else on that line
* That line is not indented beyond the indent of the first line containing the `let` keyword

For example:

```rust
let Some(x) = y.foo(
"abc",
fairly_long_identifier,
"def",
"123456",
"string",
"cheese",
) else {
bar()
}
```

Otherwise, the `else` keyword and opening brace should be placed on the next line after the end of the initializer expression, and should not be indented (the `else` keyword should be aligned with the `let` keyword).

For example:

```rust
let Some(x) = abcdef()
.foo(
"abc",
some_really_really_really_long_ident,
"ident",
"123456",
)
.bar()
.baz()
.qux("fffffffffffffffff")
else {
foo_bar()
}
```

##### Single line let-else statements

The entire let-else statement may be formatted on a single line if all the following are true:

* the entire statement is *short*
* the `else` block contains a single-line expression and no statements
* the `else` block contains no comments
* the let statement components preceding the `else` block can be formatted on a single line

```rust
let Some(1) = opt else { return };

let Some(1) = opt else {
return;
};

let Some(1) = opt else {
// nope
return
};
```

Formatters may allow users to configure the value of the threshold
used to determine whether a let-else statement is *short*.

### Macros in statement position

Expand Down
17 changes: 13 additions & 4 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,11 @@ so that we can apply CSS-filters to change the arrow color in themes */
background-repeat: no-repeat;
background-size: 20px;
background-position: calc(100% - 2px) 56%;
/* image is black color */
background-image: url("down-arrow-927217e04c7463ac.svg");
/* down arrow (image is black color) */
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" \
width="128" height="128" viewBox="-30 -20 176 176"><path d="M111,40.5L64,87.499L17,40.5" \
fill="none" stroke="black" strike-linecap="square" stroke-miterlimit="10" stroke-width="12"/> \
</svg>');
/* changes the arrow image color */
filter: var(--crate-search-div-filter);
}
Expand Down Expand Up @@ -1444,7 +1447,10 @@ details.toggle > summary.hideme > span {
}

details.toggle > summary::before {
background: url("toggle-plus-1092eb4930d581b0.svg") no-repeat top left;
/* toggle plus */
background: url('data:image/svg+xml,<svg width="17" height="17" \
shape-rendering="crispEdges" stroke="black" fill="none" xmlns="http://www.w3.org/2000/svg"><path \
d="M5 2.5H2.5v12H5m7-12h2.5v12H12M5 8.5h7M8.5 12V8.625v0V5"/></svg>') no-repeat top left;
content: "";
cursor: pointer;
width: 16px;
Expand Down Expand Up @@ -1522,7 +1528,10 @@ details.toggle[open] > summary.hideme > span {
}

details.toggle[open] > summary::before {
background: url("toggle-minus-31bbd6e4c77f5c96.svg") no-repeat top left;
/* toggle minus */
background: url('data:image/svg+xml,<svg width="17" height="17" \
shape-rendering="crispEdges" stroke="black" fill="none" xmlns="http://www.w3.org/2000/svg"><path \
d="M5 2.5H2.5v12H5m7-12h2.5v12H12M5 8.5h7"/></svg>') no-repeat top left;
}

details.toggle[open] > summary::after {
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/html/static/images/down-arrow.svg

This file was deleted.

1 change: 0 additions & 1 deletion src/librustdoc/html/static/images/toggle-minus.svg

This file was deleted.

1 change: 0 additions & 1 deletion src/librustdoc/html/static/images/toggle-plus.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ static_files! {
scrape_examples_js => "static/js/scrape-examples.js",
wheel_svg => "static/images/wheel.svg",
clipboard_svg => "static/images/clipboard.svg",
down_arrow_svg => "static/images/down-arrow.svg",
toggle_minus_png => "static/images/toggle-minus.svg",
toggle_plus_png => "static/images/toggle-plus.svg",
copyright => "static/COPYRIGHT.txt",
license_apache => "static/LICENSE-APACHE.txt",
license_mit => "static/LICENSE-MIT.txt",
Expand Down
5 changes: 0 additions & 5 deletions tests/ui/async-await/dont-suggest-missing-await.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ LL | take_u32(x)
| |
| arguments to this function are incorrect
|
note: while checking the return type of the `async fn`
--> $DIR/dont-suggest-missing-await.rs:7:24
|
LL | async fn make_u32() -> u32 {
| ^^^ checked the `Output` of this `async fn`, found opaque type
= note: expected type `u32`
found opaque type `impl Future<Output = u32>`
note: function defined here
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/async-await/generator-desc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ LL | fun(one(), two());
| |
| arguments to this function are incorrect
|
note: while checking the return type of the `async fn`
--> $DIR/generator-desc.rs:5:16
|
LL | async fn one() {}
| ^ checked the `Output` of this `async fn`, expected opaque type
note: while checking the return type of the `async fn`
--> $DIR/generator-desc.rs:6:16
|
LL | async fn two() {}
| ^ checked the `Output` of this `async fn`, found opaque type
= note: expected opaque type `impl Future<Output = ()>` (opaque type at <$DIR/generator-desc.rs:5:16>)
found opaque type `impl Future<Output = ()>` (opaque type at <$DIR/generator-desc.rs:6:16>)
= help: consider `await`ing on both `Future`s
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/async-await/issue-61076.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ async fn struct_() -> Struct {
}

async fn tuple() -> Tuple {
//~^ NOTE checked the `Output` of this `async fn`, expected opaque type
//~| NOTE while checking the return type of the `async fn`
//~| NOTE in this expansion of desugaring of `async` block or function
Tuple(1i32)
}

Expand Down
15 changes: 5 additions & 10 deletions tests/ui/async-await/issue-61076.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | foo().await?;
| ++++++

error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> $DIR/issue-61076.rs:65:5
--> $DIR/issue-61076.rs:62:5
|
LL | t?;
| ^^ the `?` operator cannot be applied to type `T`
Expand All @@ -23,7 +23,7 @@ LL | t.await?;
| ++++++

error[E0609]: no field `0` on type `impl Future<Output = Tuple>`
--> $DIR/issue-61076.rs:74:26
--> $DIR/issue-61076.rs:71:26
|
LL | let _: i32 = tuple().0;
| ^ field not available in `impl Future`, but it is available in its `Output`
Expand All @@ -34,7 +34,7 @@ LL | let _: i32 = tuple().await.0;
| ++++++

error[E0609]: no field `a` on type `impl Future<Output = Struct>`
--> $DIR/issue-61076.rs:78:28
--> $DIR/issue-61076.rs:75:28
|
LL | let _: i32 = struct_().a;
| ^ field not available in `impl Future`, but it is available in its `Output`
Expand All @@ -45,7 +45,7 @@ LL | let _: i32 = struct_().await.a;
| ++++++

error[E0599]: no method named `method` found for opaque type `impl Future<Output = Struct>` in the current scope
--> $DIR/issue-61076.rs:82:15
--> $DIR/issue-61076.rs:79:15
|
LL | struct_().method();
| ^^^^^^ method not found in `impl Future<Output = Struct>`
Expand All @@ -56,19 +56,14 @@ LL | struct_().await.method();
| ++++++

error[E0308]: mismatched types
--> $DIR/issue-61076.rs:91:9
--> $DIR/issue-61076.rs:88:9
|
LL | match tuple() {
| ------- this expression has type `impl Future<Output = Tuple>`
LL |
LL | Tuple(_) => {}
| ^^^^^^^^ expected opaque type, found `Tuple`
|
note: while checking the return type of the `async fn`
--> $DIR/issue-61076.rs:56:21
|
LL | async fn tuple() -> Tuple {
| ^^^^^ checked the `Output` of this `async fn`, expected opaque type
= note: expected opaque type `impl Future<Output = Tuple>`
found struct `Tuple`
help: consider `await`ing on the `Future`
Expand Down
Loading