[gccrs COMMIT] gccrs: add drop bounds lint

gerris.rs@gmail.com gerris.rs@gmail.com
Mon Jul 6 02:00:42 GMT 2026


From: Lucas Ly Ba <lucas.ly-ba@outlook.com>

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 <lucas.ly-ba@outlook.com>
---
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/a7e108ec48c1b52393bc6e71b9f13ef0824663d2

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/4636

 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 ade16808e..8b6b4d6f5 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 000000000..8a24a93ef
--- /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 }

base-commit: 2c930e694e32886076ec2230789f52756ee4a997
-- 
2.54.0



More information about the Gcc-rust mailing list