about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-11-25 05:09:51 +0000
committerbors <bors@rust-lang.org>2021-11-25 05:09:51 +0000
commitc6eda7d8a7af3ef51311d3106874a7d8de994edc (patch)
treeaf4771052a4c0ebeeee0efeb373c229e941b07f9 /src
parentd2c24aabcddd1eb11af633ab6b7391ae0cd00ea2 (diff)
parent505b09e326907dcab22361be33053a4c5792f8a1 (diff)
downloadrust-c6eda7d8a7af3ef51311d3106874a7d8de994edc.tar.gz
rust-c6eda7d8a7af3ef51311d3106874a7d8de994edc.zip
Auto merge of #85346 - estebank:issue-84946, r=nagisa,varkor
Account for incorrect `impl Foo<const N: ty> {}` syntax

Fix #84946
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/parser/const-param-decl-on-type-instead-of-impl.rs15
-rw-r--r--src/test/ui/parser/const-param-decl-on-type-instead-of-impl.stderr47
-rw-r--r--src/tools/rustfmt/src/items.rs12
3 files changed, 64 insertions, 10 deletions
diff --git a/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.rs b/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.rs
new file mode 100644
index 00000000000..53e3c6f9605
--- /dev/null
+++ b/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.rs
@@ -0,0 +1,15 @@
+struct NInts<const N: usize>([u8; N]);
+impl NInts<const N: usize> {} //~ ERROR unexpected `const` parameter declaration
+
+fn main() {
+    let _: () = 42; //~ ERROR mismatched types
+}
+
+fn banana(a: <T<const N: usize>>::BAR) {}
+//~^ ERROR unexpected `const` parameter declaration
+//~| ERROR cannot find type `T` in this scope
+fn chaenomeles() {
+    path::path::Struct::<const N: usize>()
+    //~^ ERROR unexpected `const` parameter declaration
+    //~| ERROR failed to resolve: use of undeclared crate or module `path`
+}
diff --git a/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.stderr b/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.stderr
new file mode 100644
index 00000000000..96885d11ee0
--- /dev/null
+++ b/src/test/ui/parser/const-param-decl-on-type-instead-of-impl.stderr
@@ -0,0 +1,47 @@
+error: unexpected `const` parameter declaration
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:2:12
+   |
+LL | impl NInts<const N: usize> {}
+   |            ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
+   |
+help: `const` parameters must be declared for the `impl`
+   |
+LL | impl<const N: usize> NInts<N> {}
+   |     ++++++++++++++++       ~
+
+error: unexpected `const` parameter declaration
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:17
+   |
+LL | fn banana(a: <T<const N: usize>>::BAR) {}
+   |                 ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
+
+error: unexpected `const` parameter declaration
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:26
+   |
+LL |     path::path::Struct::<const N: usize>()
+   |                          ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
+
+error[E0433]: failed to resolve: use of undeclared crate or module `path`
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5
+   |
+LL |     path::path::Struct::<const N: usize>()
+   |     ^^^^ use of undeclared crate or module `path`
+
+error[E0412]: cannot find type `T` in this scope
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15
+   |
+LL | fn banana(a: <T<const N: usize>>::BAR) {}
+   |               ^ not found in this scope
+
+error[E0308]: mismatched types
+  --> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17
+   |
+LL |     let _: () = 42;
+   |            --   ^^ expected `()`, found integer
+   |            |
+   |            expected due to this
+
+error: aborting due to 6 previous errors
+
+Some errors have detailed explanations: E0308, E0412, E0433.
+For more information about an error, try `rustc --explain E0308`.
diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs
index 50121a8b6b5..acc91f861e4 100644
--- a/src/tools/rustfmt/src/items.rs
+++ b/src/tools/rustfmt/src/items.rs
@@ -2236,18 +2236,10 @@ fn rewrite_fn_base(
         result.push_str(&param_indent.to_string_with_newline(context.config));
     }
 
-    // Skip `pub(crate)`.
-    let lo_after_visibility = get_bytepos_after_visibility(fn_sig.visibility, span);
-    // A conservative estimation, the goal is to be over all parens in generics
-    let params_start = fn_sig
-        .generics
-        .params
-        .last()
-        .map_or(lo_after_visibility, |param| param.span().hi());
     let params_end = if fd.inputs.is_empty() {
         context
             .snippet_provider
-            .span_after(mk_sp(params_start, span.hi()), ")")
+            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), ")")
     } else {
         let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
         context.snippet_provider.span_after(last_span, ")")
@@ -2255,7 +2247,7 @@ fn rewrite_fn_base(
     let params_span = mk_sp(
         context
             .snippet_provider
-            .span_after(mk_sp(params_start, span.hi()), "("),
+            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), "("),
         params_end,
     );
     let param_str = rewrite_params(