Skip to content

Commit ed5e5df

Browse files
committed
E0493: showing a label where the destructor is defined.
1 parent 638b7c8 commit ed5e5df

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/librustc_mir/transform/qualify_consts.rs

+29
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::bitvec::BitVector;
1818
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1919
use rustc::dep_graph::DepNode;
2020
use rustc::hir;
21+
use rustc::hir::map as hir_map;
2122
use rustc::hir::def_id::DefId;
2223
use rustc::hir::intravisit::FnKind;
2324
use rustc::hir::map::blocks::FnLikeNode;
@@ -258,12 +259,40 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
258259
"in Nightly builds, add `#![feature(drop_types_in_const)]` \
259260
to the crate attributes to enable");
260261
} else {
262+
self.find_drop_implementation_method_span()
263+
.map(|span| err.span_label(span, &format!("destructor defined here")));
264+
261265
err.span_label(self.span, &format!("constants cannot have destructors"));
262266
}
263267

264268
err.emit();
265269
}
266270

271+
fn find_drop_implementation_method_span(&self) -> Option<Span> {
272+
self.tcx.lang_items
273+
.drop_trait()
274+
.and_then(|drop_trait_id| {
275+
let mut span = None;
276+
277+
self.tcx
278+
.lookup_trait_def(drop_trait_id)
279+
.for_each_relevant_impl(self.tcx, self.mir.return_ty, |impl_did| {
280+
self.tcx.map
281+
.as_local_node_id(impl_did)
282+
.and_then(|impl_node_id| self.tcx.map.find(impl_node_id))
283+
.map(|node| {
284+
if let hir_map::NodeItem(item) = node {
285+
if let hir::ItemImpl(_, _, _, _, _, ref methods) = item.node {
286+
span = methods.first().map(|method| method.span);
287+
}
288+
}
289+
});
290+
});
291+
292+
span
293+
})
294+
}
295+
267296
/// Check if an Lvalue with the current qualifications could
268297
/// be consumed, by either an operand or a Deref projection.
269298
fn try_consume(&mut self) -> bool {

src/test/compile-fail/E0493.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ struct Foo {
1414

1515
impl Drop for Foo {
1616
fn drop(&mut self) {}
17+
//~^ NOTE destructor defined here
18+
}
19+
20+
struct Bar {
21+
a: u32
22+
}
23+
24+
impl Drop for Bar {
25+
fn drop(&mut self) {}
1726
}
1827

1928
const F : Foo = Foo { a : 0 };

0 commit comments

Comments
 (0)