|
1 | 1 | //! A bunch of methods and structures more or less related to resolving macros and
|
2 | 2 | //! interface provided by `Resolver` to macro expander.
|
3 | 3 |
|
| 4 | +use crate::errors::{AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive}; |
4 | 5 | use crate::Namespace::*;
|
5 | 6 | use crate::{BuiltinMacroState, Determinacy};
|
6 | 7 | use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
|
@@ -543,12 +544,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
543 | 544 | };
|
544 | 545 | if let Some((article, expected)) = unexpected_res {
|
545 | 546 | let path_str = pprust::path_to_string(path);
|
546 |
| - let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str); |
547 |
| - self.tcx |
548 |
| - .sess |
549 |
| - .struct_span_err(path.span, &msg) |
550 |
| - .span_label(path.span, format!("not {} {}", article, expected)) |
551 |
| - .emit(); |
| 547 | + |
| 548 | + let mut err = MacroExpectedFound { |
| 549 | + span: path.span, |
| 550 | + expected, |
| 551 | + found: res.descr(), |
| 552 | + macro_path: &path_str, |
| 553 | + ..Default::default() // Subdiagnostics default to None |
| 554 | + }; |
| 555 | + |
| 556 | + // Suggest moving the macro out of the derive() if the macro isn't Derive |
| 557 | + if !path.span.from_expansion() |
| 558 | + && kind == MacroKind::Derive |
| 559 | + && ext.macro_kind() != MacroKind::Derive |
| 560 | + { |
| 561 | + err.remove_surrounding_derive = Some(RemoveSurroundingDerive { span: path.span }); |
| 562 | + err.add_as_non_derive = Some(AddAsNonDerive { macro_path: &path_str }); |
| 563 | + } |
| 564 | + |
| 565 | + let mut err = self.tcx.sess.create_err(err); |
| 566 | + err.span_label(path.span, format!("not {} {}", article, expected)); |
| 567 | + |
| 568 | + err.emit(); |
| 569 | + |
552 | 570 | return Ok((self.dummy_ext(kind), Res::Err));
|
553 | 571 | }
|
554 | 572 |
|
|
0 commit comments