diff options
| author | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-06-07 02:33:23 +0900 |
|---|---|---|
| committer | Takayuki Maeda <takoyaki0316@gmail.com> | 2022-06-07 09:52:52 +0900 |
| commit | 9db03b9bc8bdbc9cb0df8f022e61b97c1a6fc2ec (patch) | |
| tree | 3e85069a5a4f413d3135157e0170fa8f519d1a44 /compiler/rustc_resolve/src | |
| parent | 395a09c3dafe0c7838c9ca41d2b47bb5e79a5b6d (diff) | |
| download | rust-9db03b9bc8bdbc9cb0df8f022e61b97c1a6fc2ec.tar.gz rust-9db03b9bc8bdbc9cb0df8f022e61b97c1a6fc2ec.zip | |
suggest swapping a struct and a trait
fmt
Diffstat (limited to 'compiler/rustc_resolve/src')
| -rw-r--r-- | compiler/rustc_resolve/src/late.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 21 |
2 files changed, 29 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 73d6566e3cd..688ba89c689 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -496,6 +496,9 @@ struct DiagnosticMetadata<'ast> { /// The current impl items (used to suggest). current_impl_items: Option<&'ast [P<AssocItem>]>, + + /// When processing impl trait + currently_processing_impl_trait: Option<(TraitRef, Ty)>, } struct LateResolutionVisitor<'a, 'b, 'ast> { @@ -2066,18 +2069,22 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { fn with_optional_trait_ref<T>( &mut self, opt_trait_ref: Option<&TraitRef>, + self_type: &'ast Ty, f: impl FnOnce(&mut Self, Option<DefId>) -> T, ) -> T { let mut new_val = None; let mut new_id = None; if let Some(trait_ref) = opt_trait_ref { let path: Vec<_> = Segment::from_path(&trait_ref.path); + self.diagnostic_metadata.currently_processing_impl_trait = + Some((trait_ref.clone(), self_type.clone())); let res = self.smart_resolve_path_fragment( None, &path, PathSource::Trait(AliasPossibility::No), Finalize::new(trait_ref.ref_id, trait_ref.path.span), ); + self.diagnostic_metadata.currently_processing_impl_trait = None; if let Some(def_id) = res.base_res().opt_def_id() { new_id = Some(def_id); new_val = Some((self.r.expect_module(def_id), trait_ref.clone())); @@ -2118,7 +2125,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { this.with_self_rib(Res::SelfTy { trait_: None, alias_to: None }, |this| { this.with_lifetime_rib(LifetimeRibKind::AnonymousCreateParameter(item_id), |this| { // Resolve the trait reference, if necessary. - this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| { + this.with_optional_trait_ref(opt_trait_reference.as_ref(), self_type, |this, trait_id| { let item_def_id = this.r.local_def_id(item_id); // Register the trait definitions from here. diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index a6a04ac9ea6..2266b4a1ecb 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -145,6 +145,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { let is_expected = &|res| source.is_expected(res); let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _)); + debug!(?res, ?source); + // Make the base error. struct BaseError<'a> { msg: String, @@ -248,6 +250,25 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { let code = source.error_code(res.is_some()); let mut err = self.r.session.struct_span_err_with_code(base_error.span, &base_error.msg, code); + if let Some((trait_ref, self_ty)) = + self.diagnostic_metadata.currently_processing_impl_trait.clone() + && let TyKind::Path(_, self_ty_path) = &self_ty.kind + && let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None) + && let ModuleKind::Def(DefKind::Trait, ..) = module.kind + && trait_ref.path.span == span + && let PathSource::Trait(_) = source + && let Some(Res::Def(DefKind::Struct, _)) = res + && let Ok(self_ty_str) = + self.r.session.source_map().span_to_snippet(self_ty.span) + && let Ok(trait_ref_str) = + self.r.session.source_map().span_to_snippet(trait_ref.path.span) + { + err.multipart_suggestion( + "consider swapping the struct and the trait", + vec![(trait_ref.path.span, self_ty_str), (self_ty.span, trait_ref_str)], + Applicability::MaybeIncorrect, + ); + } if let Some(sugg) = base_error.suggestion { err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect); |
