about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/html/render/print_item.rs57
-rw-r--r--src/test/rustdoc/type-layout.rs18
-rw-r--r--src/test/ui/associated-types/issue-36499.stderr10
-rw-r--r--src/test/ui/consts/issue-88649.rs18
-rw-r--r--src/test/ui/lifetimes/lifetime-errors/issue_74400.nll.stderr30
-rw-r--r--src/test/ui/lifetimes/lifetime-errors/issue_74400.rs13
-rw-r--r--src/test/ui/lifetimes/lifetime-errors/issue_74400.stderr11
-rw-r--r--src/test/ui/parser/expr-as-stmt.fixed2
-rw-r--r--src/test/ui/parser/expr-as-stmt.rs2
-rw-r--r--src/test/ui/parser/expr-as-stmt.stderr4
-rw-r--r--src/test/ui/parser/issue-88276-unary-plus.fixed8
-rw-r--r--src/test/ui/parser/issue-88276-unary-plus.rs8
-rw-r--r--src/test/ui/parser/issue-88276-unary-plus.stderr50
13 files changed, 214 insertions, 17 deletions
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 39ef641a3ac..470749ef7b3 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -8,10 +8,12 @@ use rustc_hir as hir;
 use rustc_hir::def::CtorKind;
 use rustc_hir::def_id::DefId;
 use rustc_middle::middle::stability;
+use rustc_middle::span_bug;
 use rustc_middle::ty::layout::LayoutError;
-use rustc_middle::ty::TyCtxt;
+use rustc_middle::ty::{Adt, TyCtxt};
 use rustc_span::hygiene::MacroKind;
 use rustc_span::symbol::{kw, sym, Symbol};
+use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
 
 use super::{
     collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_strs, notable_traits_decl,
@@ -1621,6 +1623,15 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
 }
 
 fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
+    fn write_size_of_layout(w: &mut Buffer, layout: &Layout, tag_size: u64) {
+        if layout.abi.is_unsized() {
+            write!(w, "(unsized)");
+        } else {
+            let bytes = layout.size.bytes() - tag_size;
+            write!(w, "{size} byte{pl}", size = bytes, pl = if bytes == 1 { "" } else { "s" },);
+        }
+    }
+
     if !cx.shared.show_type_layout {
         return;
     }
@@ -1642,16 +1653,40 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
                  <a href=\"https://doc.rust-lang.org/reference/type-layout.html\">“Type Layout”</a> \
                  chapter for details on type layout guarantees.</p></div>"
             );
-            if ty_layout.layout.abi.is_unsized() {
-                writeln!(w, "<p><strong>Size:</strong> (unsized)</p>");
-            } else {
-                let bytes = ty_layout.layout.size.bytes();
-                writeln!(
-                    w,
-                    "<p><strong>Size:</strong> {size} byte{pl}</p>",
-                    size = bytes,
-                    pl = if bytes == 1 { "" } else { "s" },
-                );
+            w.write_str("<p><strong>Size:</strong> ");
+            write_size_of_layout(w, ty_layout.layout, 0);
+            writeln!(w, "</p>");
+            if let Variants::Multiple { variants, tag, tag_encoding, .. } =
+                &ty_layout.layout.variants
+            {
+                if !variants.is_empty() {
+                    w.write_str(
+                        "<p><strong>Size for each variant:</strong></p>\
+                            <ul>",
+                    );
+
+                    let adt = if let Adt(adt, _) = ty_layout.ty.kind() {
+                        adt
+                    } else {
+                        span_bug!(tcx.def_span(ty_def_id), "not an adt")
+                    };
+
+                    let tag_size = if let TagEncoding::Niche { .. } = tag_encoding {
+                        0
+                    } else if let Primitive::Int(i, _) = tag.value {
+                        i.size().bytes()
+                    } else {
+                        span_bug!(tcx.def_span(ty_def_id), "tag is neither niche nor int")
+                    };
+
+                    for (index, layout) in variants.iter_enumerated() {
+                        let ident = adt.variants[index].ident;
+                        write!(w, "<li><code>{name}</code>: ", name = ident);
+                        write_size_of_layout(w, layout, tag_size);
+                        writeln!(w, "</li>");
+                    }
+                    w.write_str("</ul>");
+                }
             }
         }
         // This kind of layout error can occur with valid code, e.g. if you try to
diff --git a/src/test/rustdoc/type-layout.rs b/src/test/rustdoc/type-layout.rs
index 272911de681..0868486fa59 100644
--- a/src/test/rustdoc/type-layout.rs
+++ b/src/test/rustdoc/type-layout.rs
@@ -52,3 +52,21 @@ pub struct Unsized([u8]);
 
 // @!has type_layout/trait.MyTrait.html 'Size: '
 pub trait MyTrait {}
+
+// @has type_layout/enum.Variants.html 'Size: '
+// @has - '2 bytes'
+// @has - '<code>A</code>: 0 bytes'
+// @has - '<code>B</code>: 1 byte'
+pub enum Variants {
+    A,
+    B(u8),
+}
+
+// @has type_layout/enum.WithNiche.html 'Size: '
+// @has - //p '4 bytes'
+// @has - '<code>None</code>: 0 bytes'
+// @has - '<code>Some</code>: 4 bytes'
+pub enum WithNiche {
+    None,
+    Some(std::num::NonZeroU32),
+}
diff --git a/src/test/ui/associated-types/issue-36499.stderr b/src/test/ui/associated-types/issue-36499.stderr
index ff450f60acc..610798d880f 100644
--- a/src/test/ui/associated-types/issue-36499.stderr
+++ b/src/test/ui/associated-types/issue-36499.stderr
@@ -1,8 +1,14 @@
-error: expected expression, found `+`
+error: leading `+` is not supported
   --> $DIR/issue-36499.rs:4:9
    |
 LL |     2 + +2;
-   |         ^ expected expression
+   |         ^ unexpected `+`
+   |
+help: try removing the `+`
+   |
+LL -     2 + +2;
+LL +     2 + 2;
+   | 
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/consts/issue-88649.rs b/src/test/ui/consts/issue-88649.rs
new file mode 100644
index 00000000000..43e562b5a7d
--- /dev/null
+++ b/src/test/ui/consts/issue-88649.rs
@@ -0,0 +1,18 @@
+// check-pass
+#![crate_type = "lib"]
+
+enum Foo {
+    Variant1(bool),
+    Variant2(bool),
+}
+
+const _: () = {
+    let mut n = 0;
+    while n < 2 {
+        match Foo::Variant1(true) {
+            Foo::Variant1(x) | Foo::Variant2(x) if x => {}
+            _ => {}
+        }
+        n += 1;
+    }
+};
diff --git a/src/test/ui/lifetimes/lifetime-errors/issue_74400.nll.stderr b/src/test/ui/lifetimes/lifetime-errors/issue_74400.nll.stderr
new file mode 100644
index 00000000000..5a1294f948f
--- /dev/null
+++ b/src/test/ui/lifetimes/lifetime-errors/issue_74400.nll.stderr
@@ -0,0 +1,30 @@
+error[E0310]: the parameter type `T` may not live long enough
+  --> $DIR/issue_74400.rs:12:5
+   |
+LL |     f(data, identity)
+   |     ^^^^^^^^^^^^^^^^^
+   |
+   = help: consider adding an explicit lifetime bound `T: 'static`...
+
+error[E0308]: mismatched types
+  --> $DIR/issue_74400.rs:12:5
+   |
+LL |     f(data, identity)
+   |     ^^^^^^^^^^^^^^^^^ one type is more general than the other
+   |
+   = note: expected type `for<'r> Fn<(&'r T,)>`
+              found type `Fn<(&T,)>`
+
+error: implementation of `FnOnce` is not general enough
+  --> $DIR/issue_74400.rs:12:5
+   |
+LL |     f(data, identity)
+   |     ^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
+   |
+   = note: `fn(&'2 T) -> &'2 T {identity::<&'2 T>}` must implement `FnOnce<(&'1 T,)>`, for any lifetime `'1`...
+   = note: ...but it actually implements `FnOnce<(&'2 T,)>`, for some specific lifetime `'2`
+
+error: aborting due to 3 previous errors
+
+Some errors have detailed explanations: E0308, E0310.
+For more information about an error, try `rustc --explain E0308`.
diff --git a/src/test/ui/lifetimes/lifetime-errors/issue_74400.rs b/src/test/ui/lifetimes/lifetime-errors/issue_74400.rs
new file mode 100644
index 00000000000..f83384524f7
--- /dev/null
+++ b/src/test/ui/lifetimes/lifetime-errors/issue_74400.rs
@@ -0,0 +1,13 @@
+//! Regression test for #74400: Type mismatch in function arguments E0631, E0271 are falsely
+//! recognized as E0308 mismatched types.
+
+use std::convert::identity;
+
+fn main() {}
+
+fn f<T, S>(data: &[T], key: impl Fn(&T) -> S) {
+}
+
+fn g<T>(data: &[T]) {
+    f(data, identity) //~ ERROR implementation of `FnOnce` is not general
+}
diff --git a/src/test/ui/lifetimes/lifetime-errors/issue_74400.stderr b/src/test/ui/lifetimes/lifetime-errors/issue_74400.stderr
new file mode 100644
index 00000000000..d972861a2b5
--- /dev/null
+++ b/src/test/ui/lifetimes/lifetime-errors/issue_74400.stderr
@@ -0,0 +1,11 @@
+error: implementation of `FnOnce` is not general enough
+  --> $DIR/issue_74400.rs:12:5
+   |
+LL |     f(data, identity)
+   |     ^ implementation of `FnOnce` is not general enough
+   |
+   = note: `fn(&'2 T) -> &'2 T {identity::<&'2 T>}` must implement `FnOnce<(&'1 T,)>`, for any lifetime `'1`...
+   = note: ...but it actually implements `FnOnce<(&'2 T,)>`, for some specific lifetime `'2`
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/parser/expr-as-stmt.fixed b/src/test/ui/parser/expr-as-stmt.fixed
index c217ab9774f..5f5e25991e0 100644
--- a/src/test/ui/parser/expr-as-stmt.fixed
+++ b/src/test/ui/parser/expr-as-stmt.fixed
@@ -10,7 +10,7 @@ fn foo() -> i32 {
 }
 
 fn bar() -> i32 {
-    ({2}) + 2 //~ ERROR expected expression, found `+`
+    ({2}) + 2 //~ ERROR leading `+` is not supported
     //~^ ERROR mismatched types
 }
 
diff --git a/src/test/ui/parser/expr-as-stmt.rs b/src/test/ui/parser/expr-as-stmt.rs
index b04025faaec..5428e1c32fe 100644
--- a/src/test/ui/parser/expr-as-stmt.rs
+++ b/src/test/ui/parser/expr-as-stmt.rs
@@ -10,7 +10,7 @@ fn foo() -> i32 {
 }
 
 fn bar() -> i32 {
-    {2} + 2 //~ ERROR expected expression, found `+`
+    {2} + 2 //~ ERROR leading `+` is not supported
     //~^ ERROR mismatched types
 }
 
diff --git a/src/test/ui/parser/expr-as-stmt.stderr b/src/test/ui/parser/expr-as-stmt.stderr
index ba5cd01abfc..d99e9be0000 100644
--- a/src/test/ui/parser/expr-as-stmt.stderr
+++ b/src/test/ui/parser/expr-as-stmt.stderr
@@ -9,11 +9,11 @@ help: parentheses are required to parse this as an expression
 LL |     ({2}) + {2}
    |     +   +
 
-error: expected expression, found `+`
+error: leading `+` is not supported
   --> $DIR/expr-as-stmt.rs:13:9
    |
 LL |     {2} + 2
-   |         ^ expected expression
+   |         ^ unexpected `+`
    |
 help: parentheses are required to parse this as an expression
    |
diff --git a/src/test/ui/parser/issue-88276-unary-plus.fixed b/src/test/ui/parser/issue-88276-unary-plus.fixed
new file mode 100644
index 00000000000..25b7c340f60
--- /dev/null
+++ b/src/test/ui/parser/issue-88276-unary-plus.fixed
@@ -0,0 +1,8 @@
+// run-rustfix
+#[allow(unused_parens)]
+fn main() {
+    let _ = 1; //~ ERROR leading `+` is not supported
+    let _ = (1.0 + 2.0) * 3.0; //~ ERROR leading `+` is not supported
+                           //~| ERROR leading `+` is not supported
+    let _ = [3, 4+6]; //~ ERROR leading `+` is not supported
+}
diff --git a/src/test/ui/parser/issue-88276-unary-plus.rs b/src/test/ui/parser/issue-88276-unary-plus.rs
new file mode 100644
index 00000000000..11b2e9d6016
--- /dev/null
+++ b/src/test/ui/parser/issue-88276-unary-plus.rs
@@ -0,0 +1,8 @@
+// run-rustfix
+#[allow(unused_parens)]
+fn main() {
+    let _ = +1; //~ ERROR leading `+` is not supported
+    let _ = (1.0 + +2.0) * +3.0; //~ ERROR leading `+` is not supported
+                           //~| ERROR leading `+` is not supported
+    let _ = [+3, 4+6]; //~ ERROR leading `+` is not supported
+}
diff --git a/src/test/ui/parser/issue-88276-unary-plus.stderr b/src/test/ui/parser/issue-88276-unary-plus.stderr
new file mode 100644
index 00000000000..b26761729a8
--- /dev/null
+++ b/src/test/ui/parser/issue-88276-unary-plus.stderr
@@ -0,0 +1,50 @@
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:4:13
+   |
+LL |     let _ = +1;
+   |             ^ unexpected `+`
+   |
+help: try removing the `+`
+   |
+LL -     let _ = +1;
+LL +     let _ = 1;
+   | 
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:5:20
+   |
+LL |     let _ = (1.0 + +2.0) * +3.0;
+   |                    ^ unexpected `+`
+   |
+help: try removing the `+`
+   |
+LL -     let _ = (1.0 + +2.0) * +3.0;
+LL +     let _ = (1.0 + 2.0) * +3.0;
+   | 
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:5:28
+   |
+LL |     let _ = (1.0 + +2.0) * +3.0;
+   |                            ^ unexpected `+`
+   |
+help: try removing the `+`
+   |
+LL -     let _ = (1.0 + +2.0) * +3.0;
+LL +     let _ = (1.0 + +2.0) * 3.0;
+   | 
+
+error: leading `+` is not supported
+  --> $DIR/issue-88276-unary-plus.rs:7:14
+   |
+LL |     let _ = [+3, 4+6];
+   |              ^ unexpected `+`
+   |
+help: try removing the `+`
+   |
+LL -     let _ = [+3, 4+6];
+LL +     let _ = [3, 4+6];
+   | 
+
+error: aborting due to 4 previous errors
+