Skip to content

Commit

Permalink
Tighter spans
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Mar 14, 2023
1 parent 9eae773 commit 00dc3b2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
37 changes: 35 additions & 2 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_hir::{MethodKind, Target, Unsafety};
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::builtin::{
Expand Down Expand Up @@ -2213,7 +2213,7 @@ impl CheckAttrVisitor<'_> {
tcx.fn_sig(def_id).subst(tcx, fresh_substs),
);

let cause = ObligationCause::misc(span, def_id);
let mut cause = ObligationCause::misc(span, def_id);
let sig = ocx.normalize(&cause, param_env, sig);

// proc macro is not WF.
Expand All @@ -2235,6 +2235,39 @@ impl CheckAttrVisitor<'_> {

if let Err(terr) = ocx.eq(&cause, param_env, expected_sig, sig) {
let mut diag = tcx.sess.create_err(errors::ProcMacroBadSig { span, kind });

let hir_sig = tcx.hir().fn_sig_by_hir_id(hir_id);
if let Some(hir_sig) = hir_sig {
match terr {
TypeError::ArgumentMutability(idx) | TypeError::ArgumentSorts(_, idx) => {
if let Some(ty) = hir_sig.decl.inputs.get(idx) {
diag.set_span(ty.span);
cause.span = ty.span;
} else if idx == hir_sig.decl.inputs.len() {
let span = hir_sig.decl.output.span();
diag.set_span(span);
cause.span = span;
}
}
TypeError::ArgCount => {
if let Some(ty) = hir_sig.decl.inputs.get(expected_sig.inputs().len()) {
diag.set_span(ty.span);
cause.span = ty.span;
}
}
TypeError::UnsafetyMismatch(_) => {
// FIXME: Would be nice if we had a span here..
}
TypeError::AbiMismatch(_) => {
// FIXME: Would be nice if we had a span here..
}
TypeError::VariadicMismatch(_) => {
// FIXME: Would be nice if we had a span here..
}
_ => {}
}
}

infcx.err_ctxt().note_type_err(
&mut diag,
&cause,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/signature-proc-macro-attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ LL | pub fn bad_everything(input: String) -> String {
found signature `fn(std::string::String) -> std::string::String`

error: attribute proc macro has incorrect signature
--> $DIR/signature-proc-macro-attribute.rs:28:1
--> $DIR/signature-proc-macro-attribute.rs:28:52
|
LL | pub fn too_many(a: TokenStream, b: TokenStream, c: String) -> TokenStream {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
| ^^^^^^ incorrect number of function parameters
|
= note: expected signature `fn(proc_macro::TokenStream, proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(proc_macro::TokenStream, proc_macro::TokenStream, std::string::String) -> proc_macro::TokenStream`
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/proc-macro/signature-proc-macro-derive.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
error: derive proc macro has incorrect signature
--> $DIR/signature-proc-macro-derive.rs:10:1
--> $DIR/signature-proc-macro-derive.rs:10:25
|
LL | pub fn bad_input(input: String) -> TokenStream {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(std::string::String) -> proc_macro::TokenStream`

error: derive proc macro has incorrect signature
--> $DIR/signature-proc-macro-derive.rs:16:1
--> $DIR/signature-proc-macro-derive.rs:16:42
|
LL | pub fn bad_output(input: TokenStream) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(proc_macro::TokenStream) -> std::string::String`

error: derive proc macro has incorrect signature
--> $DIR/signature-proc-macro-derive.rs:22:1
--> $DIR/signature-proc-macro-derive.rs:22:30
|
LL | pub fn bad_everything(input: String) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(std::string::String) -> std::string::String`

error: derive proc macro has incorrect signature
--> $DIR/signature-proc-macro-derive.rs:28:1
--> $DIR/signature-proc-macro-derive.rs:28:36
|
LL | pub fn too_many(a: TokenStream, b: TokenStream, c: String) -> TokenStream {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
| ^^^^^^^^^^^ incorrect number of function parameters
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(proc_macro::TokenStream, proc_macro::TokenStream, std::string::String) -> proc_macro::TokenStream`
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/proc-macro/signature-proc-macro.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
error: function-like proc macro has incorrect signature
--> $DIR/signature-proc-macro.rs:10:1
--> $DIR/signature-proc-macro.rs:10:25
|
LL | pub fn bad_input(input: String) -> TokenStream {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(std::string::String) -> proc_macro::TokenStream`

error: function-like proc macro has incorrect signature
--> $DIR/signature-proc-macro.rs:16:1
--> $DIR/signature-proc-macro.rs:16:42
|
LL | pub fn bad_output(input: TokenStream) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(proc_macro::TokenStream) -> std::string::String`

error: function-like proc macro has incorrect signature
--> $DIR/signature-proc-macro.rs:22:1
--> $DIR/signature-proc-macro.rs:22:30
|
LL | pub fn bad_everything(input: String) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
| ^^^^^^ expected `proc_macro::TokenStream`, found `std::string::String`
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(std::string::String) -> std::string::String`

error: function-like proc macro has incorrect signature
--> $DIR/signature-proc-macro.rs:28:1
--> $DIR/signature-proc-macro.rs:28:36
|
LL | pub fn too_many(a: TokenStream, b: TokenStream, c: String) -> TokenStream {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters
| ^^^^^^^^^^^ incorrect number of function parameters
|
= note: expected signature `fn(proc_macro::TokenStream) -> proc_macro::TokenStream`
found signature `fn(proc_macro::TokenStream, proc_macro::TokenStream, std::string::String) -> proc_macro::TokenStream`
Expand Down

0 comments on commit 00dc3b2

Please sign in to comment.