Skip to content

Commit

Permalink
Report missing cases of bare_trait_objects
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 18, 2021
1 parent 38ed36b commit b48530b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_typeck/src/astconv/mod.rs
Expand Up @@ -1608,6 +1608,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// the whole path.
// Will fail except for `T::A` and `Self::A`; i.e., if `qself_ty`/`qself_def` are not a type
// parameter or `Self`.
// NOTE: When this function starts resolving `Trait::AssocTy` successfully
// it should also start reportint the `BARE_TRAIT_OBJECTS` lint.
pub fn associated_path_to_ty(
&self,
hir_ref_id: hir::HirId,
Expand Down
34 changes: 32 additions & 2 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Expand Up @@ -6,14 +6,15 @@ use crate::check::callee::{self, DeferredCallResolution};
use crate::check::method::{self, MethodCallee, SelfSource};
use crate::check::{BreakableCtxt, Diverges, Expectation, FallbackMode, FnCtxt, LocalTy};

use rustc_ast::TraitObjectSyntax;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorReported};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{ExprKind, GenericArg, Node, QPath};
use rustc_hir::{ExprKind, GenericArg, Node, QPath, TyKind};
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
use rustc_infer::infer::{InferOk, InferResult};
Expand All @@ -26,7 +27,9 @@ use rustc_middle::ty::{
self, AdtKind, CanonicalUserType, DefIdTree, GenericParamDefKind, ToPolyTraitRef, ToPredicate,
Ty, UserType,
};
use rustc_session::{lint, parse::feature_err};
use rustc_session::lint;
use rustc_session::lint::builtin::BARE_TRAIT_OBJECTS;
use rustc_session::parse::feature_err;
use rustc_span::source_map::{original_sp, DUMMY_SP};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{self, BytePos, MultiSpan, Span};
Expand Down Expand Up @@ -947,6 +950,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
result
});

if result.is_ok() {
self.maybe_lint_bare_trait(qpath, hir_id);
}

// Write back the new resolution.
self.write_resolution(hir_id, result);
(
Expand All @@ -956,6 +963,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
}

fn maybe_lint_bare_trait(&self, qpath: &QPath<'_>, hir_id: hir::HirId) {
if let QPath::TypeRelative(self_ty, _) = qpath {
if let TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
self_ty.kind
{
self.tcx.struct_span_lint_hir(BARE_TRAIT_OBJECTS, hir_id, self_ty.span, |lint| {
let mut db = lint
.build(&format!("trait objects without an explicit `dyn` are deprecated"));
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span)
{
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
}
Ok(s) => (format!("<dyn {}>", s), Applicability::MachineApplicable),
Err(_) => ("<dyn <type>>".to_string(), Applicability::HasPlaceholders),
};
db.span_suggestion(self_ty.span, "use `dyn`", sugg, app);
db.emit()
});
}
}
}

/// Given a function `Node`, return its `FnDecl` if it exists, or `None` otherwise.
pub(in super::super) fn get_node_fn_decl(
&self,
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/lint/bare-trait-objects-path.rs
@@ -0,0 +1,18 @@
#![feature(associated_type_defaults)]

trait Assoc {
fn func() {}
const CONST: u8 = 0;
type Ty = u8;
}

trait Dyn {}

impl Assoc for dyn Dyn {}

fn main() {
Dyn::func(); //~ WARN trait objects without an explicit `dyn` are deprecated
::Dyn::func(); //~ WARN trait objects without an explicit `dyn` are deprecated
Dyn::CONST; //~ WARN trait objects without an explicit `dyn` are deprecated
let _: Dyn::Ty; //~ ERROR ambiguous associated type
}
29 changes: 29 additions & 0 deletions src/test/ui/lint/bare-trait-objects-path.stderr
@@ -0,0 +1,29 @@
error[E0223]: ambiguous associated type
--> $DIR/bare-trait-objects-path.rs:17:12
|
LL | let _: Dyn::Ty;
| ^^^^^^^ help: use fully-qualified syntax: `<dyn Dyn as Trait>::Ty`

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/bare-trait-objects-path.rs:14:5
|
LL | Dyn::func();
| ^^^ help: use `dyn`: `<dyn Dyn>`
|
= note: `#[warn(bare_trait_objects)]` on by default

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/bare-trait-objects-path.rs:15:5
|
LL | ::Dyn::func();
| ^^^^^ help: use `dyn`: `<dyn (::Dyn)>`

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/bare-trait-objects-path.rs:16:5
|
LL | Dyn::CONST;
| ^^^ help: use `dyn`: `<dyn Dyn>`

error: aborting due to previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0223`.

0 comments on commit b48530b

Please sign in to comment.