[COMMITTED 19/77] gccrs: add drop bounds lint

[email protected]
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.rust
Message-ID <[email protected]>
From: Lucas Ly Ba <[email protected]>

Warn when a generic parameter has an explicit `Drop` bound, which is
most likely a mistake since it does not constrain the parameter in a
useful way.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::resolve_generic_params):
	Warn on `Drop` bounds on generic parameters.

gcc/testsuite/ChangeLog:

	* rust/compile/drop-bounds_0.rs: New test.

Signed-off-by: Lucas Ly Ba <[email protected]>
---
 gcc/rust/typecheck/rust-hir-type-check-base.cc | 13 +++++++++++++
 gcc/testsuite/rust/compile/drop-bounds_0.rs    | 14 ++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/drop-bounds_0.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index ade16808e2e..8b6b4d6f543 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-type-check-base.h"
+#include "options.h"
 #include "rust-compile-base.h"
 #include "rust-hir-item.h"
 #include "rust-hir-type-check-expr.h"
@@ -715,6 +716,18 @@ TypeCheckBase::resolve_generic_params (
       auto pty = static_cast<TyTy::ParamType *> (bpty);
 
       TypeResolveGenericParam::ApplyAnyTraitBounds (type_param, pty);
+
+      // The drop_bounds lint: a `T: Drop` bound is most likely a mistake, as
+      // `Drop` bounds do not constrain a generic parameter in a useful way.
+      if (flag_unused_check_2_0)
+	if (auto drop = mappings.lookup_lang_item (LangItem::Kind::DROP))
+	  for (auto &bound : pty->get_specified_bounds ())
+	    if (bound.get_id () == drop.value ())
+	      rust_warning_at (
+		type_param.get_locus (), OPT_Wunused_variable,
+		"bounds on %<Drop%> are most likely incorrect, "
+		"use %<core::mem::needs_drop%> to detect whether "
+		"a type has a destructor");
     }
 }
 
diff --git a/gcc/testsuite/rust/compile/drop-bounds_0.rs b/gcc/testsuite/rust/compile/drop-bounds_0.rs
new file mode 100644
index 00000000000..8a24a93ef6c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/drop-bounds_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<T: Drop>(_x: T) {}
+// { dg-warning "bounds on .Drop. are most likely incorrect" "" { target *-*-* } .-1 }
-- 
2.50.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.