Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_data_structures::bitvec::BitVector;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc::dep_graph::DepNode;
use rustc::hir;
use rustc::hir::map as hir_map;
use rustc::hir::def_id::DefId;
use rustc::hir::intravisit::FnKind;
use rustc::hir::map::blocks::FnLikeNode;
Expand Down Expand Up @@ -252,14 +253,46 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {

let mut err =
struct_span_err!(self.tcx.sess, self.span, E0493, "{}", msg);

if self.mode != Mode::Const {
help!(&mut err,
"in Nightly builds, add `#![feature(drop_types_in_const)]` \
to the crate attributes to enable");
} else {
self.find_drop_implementation_method_span()
.map(|span| err.span_label(span, &format!("destructor defined here")));

err.span_label(self.span, &format!("constants cannot have destructors"));
}

err.emit();
}

fn find_drop_implementation_method_span(&self) -> Option<Span> {
self.tcx.lang_items
.drop_trait()
.and_then(|drop_trait_id| {
let mut span = None;

self.tcx
.lookup_trait_def(drop_trait_id)
.for_each_relevant_impl(self.tcx, self.mir.return_ty, |impl_did| {
self.tcx.map
.as_local_node_id(impl_did)
.and_then(|impl_node_id| self.tcx.map.find(impl_node_id))
.map(|node| {
if let hir_map::NodeItem(item) = node {
if let hir::ItemImpl(_, _, _, _, _, ref methods) = item.node {
span = methods.first().map(|method| method.span);
}
}
});
});

span
})
}

/// Check if an Lvalue with the current qualifications could
/// be consumed, by either an operand or a Deref projection.
fn try_consume(&mut self) -> bool {
Expand Down
13 changes: 12 additions & 1 deletion src/test/compile-fail/E0493.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ struct Foo {

impl Drop for Foo {
fn drop(&mut self) {}
//~^ NOTE destructor defined here
}

const F : Foo = Foo { a : 0 }; //~ ERROR E0493
struct Bar {
a: u32
}

impl Drop for Bar {
fn drop(&mut self) {}
}

const F : Foo = Foo { a : 0 };
//~^ ERROR constants are not allowed to have destructors [E0493]
//~| NOTE constants cannot have destructors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we're updating the span, can you move this test over to somewhere in the src/test/ui directory so that we can test the output?

UI tests are pretty straightforward. The compiler runs them then compares the output with the corresponding .stderr file. If they match, it passes. You can look at the other .stderr files to see how it's done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I didn't know about this. :) Done!


fn main() {
}