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 8 pull requests #105775

Merged
merged 17 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
8 changes: 1 addition & 7 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ enum SelfSemantic {
/// What is the context that prevents using `~const`?
enum DisallowTildeConstContext<'a> {
TraitObject,
ImplTrait,
Fn(FnKind<'a>),
}

Expand Down Expand Up @@ -187,11 +186,7 @@ impl<'a> AstValidator<'a> {

fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
let old = mem::replace(&mut self.outer_impl_trait, outer);
if outer.is_some() {
self.with_banned_tilde_const(DisallowTildeConstContext::ImplTrait, f);
} else {
f(self);
}
f(self);
self.outer_impl_trait = old;
}

Expand Down Expand Up @@ -1384,7 +1379,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
let mut err = self.err_handler().struct_span_err(bound.span(), "`~const` is not allowed here");
match reason {
DisallowTildeConstContext::TraitObject => err.note("trait objects cannot have `~const` trait bounds"),
DisallowTildeConstContext::ImplTrait => err.note("`impl Trait`s cannot have `~const` trait bounds"),
DisallowTildeConstContext::Fn(FnKind::Closure(..)) => err.note("closures cannot have `~const` trait bounds"),
DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => err.span_note(ident.span, "this function is not `const`, so it cannot have `~const` trait bounds"),
};
Expand Down
53 changes: 34 additions & 19 deletions compiler/rustc_error_codes/src/error_codes/E0158.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
An associated const has been referenced in a pattern.
An associated `const`, `const` parameter or `static` has been referenced
in a pattern.

Erroneous code example:

```compile_fail,E0158
enum EFoo { A, B, C, D }
enum Foo {
One,
Two
}

trait Foo {
const X: EFoo;
trait Bar {
const X: Foo;
}

fn test<A: Foo>(arg: EFoo) {
fn test<A: Bar>(arg: Foo) {
match arg {
A::X => { // error!
println!("A::X");
}
A::X => println!("A::X"), // error: E0158: associated consts cannot be
// referenced in patterns
Foo::Two => println!("Two")
}
}
```

`const` and `static` mean different things. A `const` is a compile-time
constant, an alias for a literal value. This property means you can match it
directly within a pattern.
Associated `const`s cannot be referenced in patterns because it is impossible
for the compiler to prove exhaustiveness (that some pattern will always match).
Take the above example, because Rust does type checking in the *generic*
method, not the *monomorphized* specific instance. So because `Bar` could have
theoretically infinite implementations, there's no way to always be sure that
`A::X` is `Foo::One`. So this code must be rejected. Even if code can be
proven exhaustive by a programmer, the compiler cannot currently prove this.

The `static` keyword, on the other hand, guarantees a fixed location in memory.
This does not always mean that the value is constant. For example, a global
mutex can be declared `static` as well.
The same holds true of `const` parameters and `static`s.

If you want to match against a `static`, consider using a guard instead:
If you want to match against an associated `const`, `const` parameter or
`static` consider using a guard instead:

```
static FORTY_TWO: i32 = 42;
trait Trait {
const X: char;
}

static FOO: char = 'j';

match Some(42) {
Some(x) if x == FORTY_TWO => {}
_ => {}
fn test<A: Trait, const Y: char>(arg: char) {
match arg {
c if c == A::X => println!("A::X"),
c if c == Y => println!("Y"),
c if c == FOO => println!("FOO"),
_ => ()
}
}
```
10 changes: 10 additions & 0 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
}
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
span: item_span,
..
})) => {
tcx.sess.delay_span_bug(
*item_span,
"auto trait is invoked with no method error, but no error reported?",
);
}
Some(_) => unreachable!(),
None => (),
}
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// The deque is assumed to be partitioned according to the given predicate.
/// This means that all elements for which the predicate returns true are at the start of the deque
/// and all elements for which the predicate returns false are at the end.
/// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0
/// For example, `[7, 15, 3, 5, 4, 12, 6]` is partitioned under the predicate `x % 2 != 0`
/// (all odd numbers are at the start, all even at the end).
///
/// If the deque is not partitioned, the returned result is unspecified and meaningless,
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
/// if any element creation was unsuccessful.
///
/// The return type of this function depends on the return type of the closure.
/// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N]; E>`.
/// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`.
/// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`.
///
/// # Arguments
Expand Down Expand Up @@ -522,7 +522,7 @@ impl<T, const N: usize> [T; N] {
/// return an array the same size as `self` or the first error encountered.
///
/// The return type of this function depends on the return type of the closure.
/// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N]; E>`.
/// If you return `Result<T, E>` from the closure, you'll get a `Result<[T; N], E>`.
/// If you return `Option<T>` from the closure, you'll get an `Option<[T; N]>`.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,7 @@ pub trait Iterator {
/// the first true result or the first error.
///
/// The return type of this method depends on the return type of the closure.
/// If you return `Result<bool, E>` from the closure, you'll get a `Result<Option<Self::Item>; E>`.
/// If you return `Result<bool, E>` from the closure, you'll get a `Result<Option<Self::Item>, E>`.
/// If you return `Option<bool>` from the closure, you'll get an `Option<Option<Self::Item>>`.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3795,7 +3795,7 @@ impl<T> [T] {
/// The slice is assumed to be partitioned according to the given predicate.
/// This means that all elements for which the predicate returns true are at the start of the slice
/// and all elements for which the predicate returns false are at the end.
/// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0
/// For example, `[7, 15, 3, 5, 4, 12, 6]` is partitioned under the predicate `x % 2 != 0`
/// (all odd numbers are at the start, all even at the end).
///
/// If this slice is not partitioned, the returned result is unspecified and meaningless,
Expand Down
38 changes: 16 additions & 22 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,15 @@ img {
overflow-y: hidden;
}

.source .sidebar, #sidebar-toggle, #source-sidebar {
.source .sidebar, #src-sidebar-toggle, #source-sidebar {
background-color: var(--sidebar-background-color);
}

#sidebar-toggle > button:hover, #sidebar-toggle > button:focus {
#src-sidebar-toggle > button:hover, #src-sidebar-toggle > button:focus {
background-color: var(--sidebar-background-color-hover);
}

.source .sidebar > *:not(#sidebar-toggle) {
.source .sidebar > *:not(#src-sidebar-toggle) {
visibility: hidden;
}

Expand All @@ -413,7 +413,7 @@ img {
flex-basis: 300px;
}

.source-sidebar-expanded .source .sidebar > *:not(#sidebar-toggle) {
.source-sidebar-expanded .source .sidebar > *:not(#src-sidebar-toggle) {
visibility: visible;
}

Expand Down Expand Up @@ -1118,8 +1118,7 @@ pre.rust .doccomment {
top: 5px;
}

.example-wrap .tooltip::after {
display: none;
.example-wrap .tooltip:hover::after {
text-align: center;
padding: 5px 3px 3px 3px;
border-radius: 6px;
Expand All @@ -1134,35 +1133,30 @@ pre.rust .doccomment {
color: var(--tooltip-color);
}

.example-wrap .tooltip::before {
.example-wrap .tooltip:hover::before {
content: " ";
position: absolute;
top: 50%;
left: 16px;
margin-top: -5px;
display: none;
z-index: 1;
border: 5px solid transparent;
border-right-color: var(--tooltip-background-color);
}

.example-wrap.ignore .tooltip::after {
.example-wrap.ignore .tooltip:hover::after {
content: "This example is not tested";
}
.example-wrap.compile_fail .tooltip::after {
.example-wrap.compile_fail .tooltip:hover::after {
content: "This example deliberately fails to compile";
}
.example-wrap.should_panic .tooltip::after {
.example-wrap.should_panic .tooltip:hover::after {
content: "This example panics";
}
.example-wrap.edition .tooltip::after {
.example-wrap.edition .tooltip:hover::after {
content: "This code runs with edition " attr(data-edition);
}

.example-wrap .tooltip:hover::before, .example-wrap .tooltip:hover::after {
display: inline;
}

.example-wrap.compile_fail .tooltip,
.example-wrap.should_panic .tooltip,
.example-wrap.ignore .tooltip {
Expand Down Expand Up @@ -1295,7 +1289,7 @@ a.test-arrow:hover {
font-size: 1rem;
}

#sidebar-toggle {
#src-sidebar-toggle {
position: sticky;
top: 0;
left: 0;
Expand Down Expand Up @@ -1324,7 +1318,7 @@ a.test-arrow:hover {
#source-sidebar div.files > a.selected {
background-color: var(--source-sidebar-background-selected);
}
#sidebar-toggle > button {
#src-sidebar-toggle > button {
font-size: inherit;
font-weight: bold;
background: none;
Expand Down Expand Up @@ -1726,7 +1720,7 @@ in storage.js
left: -11px;
}

#sidebar-toggle {
#src-sidebar-toggle {
position: fixed;
left: 1px;
top: 100px;
Expand All @@ -1740,7 +1734,7 @@ in storage.js
border-left: 0;
}

.source-sidebar-expanded #sidebar-toggle {
.source-sidebar-expanded #src-sidebar-toggle {
left: unset;
top: unset;
width: unset;
Expand Down Expand Up @@ -1851,10 +1845,10 @@ in storage.js
width: 35px;
}

#sidebar-toggle {
#src-sidebar-toggle {
top: 10px;
}
.source-sidebar-expanded #sidebar-toggle {
.source-sidebar-expanded #src-sidebar-toggle {
top: unset;
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustdoc/html/static/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@
cursor: pointer;
}

.setting-line > .sub-settings {
padding-left: 42px;
width: 100%;
display: block;
}

#settings .setting-line {
margin: 1.2em 0.6em;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/js/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function toggleSidebar() {

function createSidebarToggle() {
const sidebarToggle = document.createElement("div");
sidebarToggle.id = "sidebar-toggle";
sidebarToggle.id = "src-sidebar-toggle";

const inner = document.createElement("button");

Expand Down
4 changes: 2 additions & 2 deletions src/test/rustdoc-gui/code-sidebar-toggle.goml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This test checks that the source code pages sidebar toggle is working as expected.
goto: "file://" + |DOC_PATH| + "/test_docs/index.html"
click: ".srclink"
wait-for: "#sidebar-toggle"
click: "#sidebar-toggle"
wait-for: "#src-sidebar-toggle"
click: "#src-sidebar-toggle"
fail: true
assert-css: ("#source-sidebar", { "left": "-300px" })
6 changes: 3 additions & 3 deletions src/test/rustdoc-gui/codeblock-tooltip.goml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define-function: (
{"border-left": "2px solid rgba(255, 0, 0, 0.5)"},
)),

("move-cursor-to", ".docblock .example-wrap.compile_fail"),
("move-cursor-to", ".docblock .example-wrap.compile_fail .tooltip"),

("assert-css", (
".docblock .example-wrap.compile_fail .tooltip",
Expand Down Expand Up @@ -60,7 +60,7 @@ define-function: (
{"border-left": "2px solid rgba(255, 0, 0, 0.5)"},
)),

("move-cursor-to", ".docblock .example-wrap.should_panic"),
("move-cursor-to", ".docblock .example-wrap.should_panic .tooltip"),

("assert-css", (
".docblock .example-wrap.should_panic .tooltip",
Expand Down Expand Up @@ -100,7 +100,7 @@ define-function: (
{"border-left": "2px solid rgba(255, 142, 0, 0.6)"},
)),

("move-cursor-to", ".docblock .example-wrap.ignore"),
("move-cursor-to", ".docblock .example-wrap.ignore .tooltip"),

("assert-css", (
".docblock .example-wrap.ignore .tooltip",
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc-gui/cursor.goml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ assert-css: (".sidebar-menu-toggle", {"cursor": "pointer"})

// the sidebar toggle button on the source code pages
goto: "file://" + |DOC_PATH| + "/src/lib2/lib.rs.html"
assert-css: ("#sidebar-toggle > button", {"cursor": "pointer"})
assert-css: ("#src-sidebar-toggle > button", {"cursor": "pointer"})
Loading