Skip to content

Commit 63168f7

Browse files
committed
Lint bare traits
1 parent bd29696 commit 63168f7

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/librustc/hir/lowering.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use hir::HirVec;
4646
use hir::map::{Definitions, DefKey, DefPathData};
4747
use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX, DefIndexAddressSpace};
4848
use hir::def::{Def, PathResolution};
49-
use lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
49+
use lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES};
5050
use middle::cstore::CrateStore;
5151
use rustc_data_structures::indexed_vec::IndexVec;
5252
use session::Session;
@@ -912,7 +912,11 @@ impl<'a> LoweringContext<'a> {
912912
TyKind::Path(ref qself, ref path) => {
913913
let id = self.lower_node_id(t.id);
914914
let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit, itctx);
915-
return self.ty_path(id, t.span, qpath);
915+
let ty = self.ty_path(id, t.span, qpath);
916+
if let hir::TyTraitObject(..) = ty.node {
917+
self.maybe_lint_bare_trait(t.span, t.id);
918+
}
919+
return ty;
916920
}
917921
TyKind::ImplicitSelf => {
918922
hir::TyPath(hir::QPath::Resolved(None, P(hir::Path {
@@ -931,7 +935,7 @@ impl<'a> LoweringContext<'a> {
931935
let expr = self.lower_body(None, |this| this.lower_expr(expr));
932936
hir::TyTypeof(expr)
933937
}
934-
TyKind::TraitObject(ref bounds, ..) => {
938+
TyKind::TraitObject(ref bounds, kind) => {
935939
let mut lifetime_bound = None;
936940
let bounds = bounds.iter().filter_map(|bound| {
937941
match *bound {
@@ -950,6 +954,9 @@ impl<'a> LoweringContext<'a> {
950954
let lifetime_bound = lifetime_bound.unwrap_or_else(|| {
951955
self.elided_lifetime(t.span)
952956
});
957+
if kind != TraitObjectSyntax::Dyn {
958+
self.maybe_lint_bare_trait(t.span, t.id);
959+
}
953960
hir::TyTraitObject(bounds, lifetime_bound)
954961
}
955962
TyKind::ImplTrait(ref bounds) => {
@@ -3685,7 +3692,6 @@ impl<'a> LoweringContext<'a> {
36853692
// The original ID is taken by the `PolyTraitRef`,
36863693
// so the `Ty` itself needs a different one.
36873694
id = self.next_id();
3688-
36893695
hir::TyTraitObject(hir_vec![principal], self.elided_lifetime(span))
36903696
} else {
36913697
hir::TyPath(hir::QPath::Resolved(None, path))
@@ -3703,6 +3709,16 @@ impl<'a> LoweringContext<'a> {
37033709
name: hir::LifetimeName::Implicit,
37043710
}
37053711
}
3712+
3713+
fn maybe_lint_bare_trait(&self, span: Span, id: NodeId) {
3714+
if self.sess.features.borrow().dyn_trait {
3715+
self.sess.buffer_lint_with_diagnostic(
3716+
builtin::BARE_TRAIT_OBJECT, id, span,
3717+
"trait objects without an explicit `dyn` are deprecated",
3718+
builtin::BuiltinLintDiagnostics::BareTraitObject(span)
3719+
)
3720+
}
3721+
}
37063722
}
37073723

37083724
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {

src/librustc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
html_root_url = "https://doc.rust-lang.org/nightly/")]
4141
#![deny(warnings)]
4242

43+
#![cfg_attr(not(stage0), allow(bare_trait_object))]
44+
4345
#![feature(box_patterns)]
4446
#![feature(box_syntax)]
4547
#![feature(conservative_impl_trait)]

src/librustc_mir/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
1515
*/
1616

1717
#![deny(warnings)]
18+
#![cfg_attr(not(stage0), allow(bare_trait_object))]
1819

1920
#![feature(box_patterns)]
2021
#![feature(box_syntax)]

0 commit comments

Comments
 (0)