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

Underscore-named parameter makes hint point at invalid token #66802

Closed
Patryk27 opened this issue Nov 27, 2019 · 3 comments · Fixed by #67665
Closed

Underscore-named parameter makes hint point at invalid token #66802

Patryk27 opened this issue Nov 27, 2019 · 3 comments · Fixed by #67665
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Nov 27, 2019

Hi,

I've got following code:

fn foo1<T: ?Sized>(_: T) {
    // here be dragons
}

fn foo2<T>(_: T) where T: ?Sized {
    // here be dragons
}

... that returns:

error[E0277]: the size for values of type `T` cannot be known at compilation time
 --> src/lib.rs:1:26
  |
1 | fn foo1<T: ?Sized>(_: T) {
  |         --               ^ doesn't have a size known at compile-time

error[E0277]: the size for values of type `T` cannot be known at compilation time
 --> src/lib.rs:5:18
  |
5 | fn foo2<T>(_: T) where T: ?Sized {
  |                  ^              - help: consider further restricting type parameter `T`: `, T: std::marker::Sized`
  |                  |
  |                  doesn't have a size known at compile-time

(playground)

In both cases the tip points at the very next token after the parameter list, instead of the underscore. Additionally, the foo2's help message (consider further restricting ...) has broken formatting.

I'd like to fix this myself, if only you'd give me a hint where to look :-)

Thanks;

@hellow554
Copy link
Contributor

Interesting enough:

Before 1.15 ?Sized isn't valid.
From 1.15 to 1.19 this compiles just fine
Beginning with 1.20 the alignment is wrong.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 27, 2019
@VirrageS
Copy link
Contributor

@Patryk27 If you still want to look into this problem you may find this diff helpful:

diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs
index 96d42dd73b1..0e021d658aa 100644
--- a/src/librustc/traits/error_reporting.rs
+++ b/src/librustc/traits/error_reporting.rs
@@ -1176,7 +1176,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
                             // `fn foo<T>(t: T) where T: Debug {}` →
                             // `fn foo<T>(t: T) where T: Debug, T: Trait {}`
                             err.span_suggestion(
-                                generics.where_clause.span().unwrap().shrink_to_hi(),
+                                generics.where_clause.predicates, // TODO: fixme
                                 &format!(
                                     "consider further restricting type parameter `{}`",
                                     param_name,
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 9107e993311..83a90e64eb1 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -1229,7 +1229,7 @@ fn check_fn<'a, 'tcx>(
         // for simple cases like `fn foo(x: Trait)`,
         // where we would error once on the parameter as a whole, and once on the binding `x`.
         if param.pat.simple_ident().is_none() && !fcx.tcx.features().unsized_locals {
-            fcx.require_type_is_sized(param_ty, decl.output.span(), traits::SizedArgumentType);
+            fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType);
         }

         fcx.write_ty(param.hir_id, param_ty);

I've changed the decl.output.span() to param.pat.span and now it seems working correctly:

error[E0277]: the size for values of type `T` cannot be known at compilation time
  --> $DIR/unsized-fn-param.rs:1:20
   |
LL | fn foo1<T: ?Sized>(_: T) {
   |         --         ^ doesn't have a size known at compile-time
   |         |
   |         help: consider further restricting this bound: `T: std::marker::Sized +`
   |
   = help: the trait `std::marker::Sized` is not implemented for `T`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: all function arguments must have a statically known size
   = help: unsized locals are gated as an unstable feature

as far for the HELP message in foo2 it probably needs iterating through generics.where_clause.predicates finding right predicate with correct name and print the span for it (if not found we should probably default generics.where_clause.span().unwrap().shrink_to_hi() or to generics.where_clause.span().unwrap().shrink_to_lo() or maybe find a better heuristic).

I'm not sure if these changes don't break other tests so be sure to run whole suite.

@Patryk27
Copy link
Contributor Author

Patryk27 commented Nov 29, 2019

Great - thanks! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants