From: Lucas Ly Ba <[email protected]>
Warn on a trait object that carries a `Drop` bound, as values are
dropped automatically and the bound has no effect.
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Warn on
a trait object with a `Drop` bound.
* rust-lang.cc (grs_langhook_init_options_struct): Enable warn_unused.
gcc/testsuite/ChangeLog:
* rust/compile/dyn-drop_0.rs: New test.
Signed-off-by: Lucas Ly Ba <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/a9a86ed4b0370586700fa2112bf5736ed50a24b8
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4641
gcc/rust/rust-lang.cc | 2 ++
gcc/rust/typecheck/rust-hir-type-check-type.cc | 10 ++++++++++
gcc/testsuite/rust/compile/dyn-drop_0.rs | 14 ++++++++++++++
3 files changed, 26 insertions(+)
create mode 100644 gcc/testsuite/rust/compile/dyn-drop_0.rs
diff --git a/gcc/rust/rust-lang.cc b/gcc/rust/rust-lang.cc
index dde3ef204..dc39bc053 100644
--- a/gcc/rust/rust-lang.cc
+++ b/gcc/rust/rust-lang.cc
@@ -136,6 +136,8 @@ grs_langhook_init_options_struct (struct gcc_options *opts)
/* We need to warn on unused variables by default */
opts->x_warn_unused_variable = 1;
+ /* Experimental lints under -frust-unused-check-2.0 warn by default */
+ opts->x_warn_unused = 1;
/* For const variables too */
opts->x_warn_unused_const_variable = 1;
/* And finally unused result for #[must_use] */
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 634d04988..c6fbb2ae1 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -655,6 +655,16 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
specified_bounds.push_back (std::move (predicate));
}
+ // The dyn_drop lint: a trait object with a `Drop` bound is pointless, as
+ // values are dropped automatically regardless of the bound.
+ if (flag_unused_check_2_0)
+ if (auto drop = mappings.lookup_lang_item (LangItem::Kind::DROP))
+ for (auto &bound : specified_bounds)
+ if (bound.get_id () == drop.value ())
+ rust_warning_at (type.get_locus (), OPT_Wunused,
+ "this trait object has a %<Drop%> bound, which has "
+ "no effect");
+
RustIdent ident{CanonicalPath::create_empty (), type.get_locus ()};
translated
= new TyTy::DynamicObjectType (type.get_mappings ().get_hirid (), ident,
diff --git a/gcc/testsuite/rust/compile/dyn-drop_0.rs
b/gcc/testsuite/rust/compile/dyn-drop_0.rs
new file mode 100644
index 000000000..07bc30d7b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/dyn-drop_0.rs
@@ -0,0 +1,14 @@
+// { dg-additional-options "-frust-unused-check-2.0" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "drop"]
+pub trait Drop {
+ fn drop(&mut self);
+}
+
+pub fn f(_x: &dyn Drop) {}
+// { dg-warning "trait object has a .Drop. bound" "" { target *-*-* } .-1 }
base-commit: dee8434fe7cd203275e65dcfb6813afcef77e906
--
2.54.0