https://gcc.gnu.org/g:be9834cd7b368c924c68eddf8dcb10b83422d662
commit r17-3077-gbe9834cd7b368c924c68eddf8dcb10b83422d662 Author: Lucas Ly Ba <[email protected]> Date: Sat Jun 27 19:07:05 2026 +0200 gccrs: add drop bounds lint 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]> Diff: --- gcc/rust/typecheck/rust-hir-type-check-base.cc | 13 +++++++++++++ gcc/testsuite/rust/compile/drop-bounds_0.rs | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index ade16808e2e7..8b6b4d6f5431 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 000000000000..8a24a93ef6ca --- /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 }
