Skip to content

Commit d3bc129

Browse files
committed
Auto merge of rust-lang#94706 - matthiaskrgr:rollup-l5erynr, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#93350 (libunwind: readd link attrs to _Unwind_Backtrace) - rust-lang#93827 (Stabilize const_fn_fn_ptr_basics, const_fn_trait_bound, and const_impl_trait) - rust-lang#94696 (Remove whitespaces and use CSS to align line numbers to the right instead) - rust-lang#94700 (rustdoc: Update minifier version) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents b36924b + 43ce0a9 commit d3bc129

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

clippy_lints/src/missing_const_for_fn.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::qualify_min_const_fn::is_min_const_fn;
33
use clippy_utils::ty::has_drop;
44
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, msrvs, trait_ref_of_method};
55
use rustc_hir as hir;
6+
use rustc_hir::def_id::CRATE_DEF_ID;
67
use rustc_hir::intravisit::FnKind;
78
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
89
use rustc_lint::{LateContext, LateLintPass};
@@ -131,6 +132,18 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
131132
FnKind::Closure => return,
132133
}
133134

135+
// Const fns are not allowed as methods in a trait.
136+
{
137+
let parent = cx.tcx.hir().get_parent_item(hir_id);
138+
if parent != CRATE_DEF_ID {
139+
if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent) {
140+
if let hir::ItemKind::Trait(..) = &item.kind {
141+
return;
142+
}
143+
}
144+
}
145+
}
146+
134147
let mir = cx.tcx.optimized_mir(def_id);
135148

136149
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) {

clippy_utils/src/qualify_min_const_fn.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,12 @@ pub fn is_min_const_fn<'a, 'tcx>(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, msrv:
3232
| ty::PredicateKind::Projection(_)
3333
| ty::PredicateKind::ConstEvaluatable(..)
3434
| ty::PredicateKind::ConstEquate(..)
35+
| ty::PredicateKind::Trait(..)
3536
| ty::PredicateKind::TypeWellFormedFromEnv(..) => continue,
3637
ty::PredicateKind::ObjectSafe(_) => panic!("object safe predicate on function: {:#?}", predicate),
3738
ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {:#?}", predicate),
3839
ty::PredicateKind::Subtype(_) => panic!("subtype predicate on function: {:#?}", predicate),
3940
ty::PredicateKind::Coerce(_) => panic!("coerce predicate on function: {:#?}", predicate),
40-
ty::PredicateKind::Trait(pred) => {
41-
if Some(pred.def_id()) == tcx.lang_items().sized_trait() {
42-
continue;
43-
}
44-
match pred.self_ty().kind() {
45-
ty::Param(ref p) => {
46-
let generics = tcx.generics_of(current);
47-
let def = generics.type_param(p, tcx);
48-
let span = tcx.def_span(def.def_id);
49-
return Err((
50-
span,
51-
"trait bounds other than `Sized` \
52-
on const fn parameters are unstable"
53-
.into(),
54-
));
55-
},
56-
// other kinds of bounds are either tautologies
57-
// or cause errors in other passes
58-
_ => continue,
59-
}
60-
},
6141
}
6242
}
6343
match predicates.parent {

tests/ui/missing_const_for_fn/could_be_const.rs

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ fn sub(x: u32) -> usize {
4949
unsafe { transmute(&x) }
5050
}
5151

52-
// NOTE: This is currently not yet allowed to be const
53-
// Once implemented, Clippy should be able to suggest this as const, too.
5452
fn generic_arr<T: Copy>(t: [T; 1]) -> T {
5553
t[0]
5654
}

tests/ui/missing_const_for_fn/could_be_const.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,28 @@ LL | | }
5858
| |_^
5959

6060
error: this could be a `const fn`
61-
--> $DIR/could_be_const.rs:67:9
61+
--> $DIR/could_be_const.rs:52:1
62+
|
63+
LL | / fn generic_arr<T: Copy>(t: [T; 1]) -> T {
64+
LL | | t[0]
65+
LL | | }
66+
| |_^
67+
68+
error: this could be a `const fn`
69+
--> $DIR/could_be_const.rs:65:9
6270
|
6371
LL | / pub fn b(self, a: &A) -> B {
6472
LL | | B
6573
LL | | }
6674
| |_________^
6775

6876
error: this could be a `const fn`
69-
--> $DIR/could_be_const.rs:77:5
77+
--> $DIR/could_be_const.rs:75:5
7078
|
7179
LL | / fn const_fn_stabilized_before_msrv(byte: u8) {
7280
LL | | byte.is_ascii_digit();
7381
LL | | }
7482
| |_____^
7583

76-
error: aborting due to 9 previous errors
84+
error: aborting due to 10 previous errors
7785

0 commit comments

Comments
 (0)