Skip to content

Commit 354502a

Browse files
committed
Lint unstable extern fns that are const stable
1 parent 97816a2 commit 354502a

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

compiler/rustc_lint/src/internal.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,29 @@ impl LateLintPass<'_> for IncompatibleStability {
401401
lint.build("functions cannot be const-stable if they are unstable").emit();
402402
});
403403
}
404+
405+
fn check_foreign_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::ForeignItem<'_>) {
406+
use rustc_attr::{ConstStability, Stability, StabilityLevel};
407+
408+
if !matches!(item.kind, rustc_hir::ForeignItemKind::Fn(..)) {
409+
return;
410+
}
411+
412+
if !matches!(
413+
cx.tcx.lookup_stability(item.def_id),
414+
Some(Stability { level: StabilityLevel::Unstable { .. }, .. })
415+
) {
416+
return;
417+
}
418+
if !matches!(
419+
cx.tcx.lookup_const_stability(item.def_id),
420+
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. })
421+
) {
422+
return;
423+
}
424+
425+
cx.struct_span_lint(INCOMPATIBLE_STABILITY, item.span, |lint| {
426+
lint.build("functions cannot be const-stable if they are unstable").emit();
427+
});
428+
}
404429
}

src/test/ui/lint/incompatible_stability.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// compile-flags: -Zunstable-options
22
#![stable(feature = "stable", since = "1.0.0")]
3-
#![feature(staged_api, rustc_attrs)]
3+
#![feature(staged_api, rustc_attrs, intrinsics)]
44

55
#[unstable(feature = "unstable", issue = "none")]
66
#[rustc_const_stable(feature = "stable", since = "1.0.0")]
@@ -13,4 +13,12 @@ mod bar {
1313
const fn foo() {} //~ ERROR functions cannot be const-stable if they are unstable
1414
}
1515

16+
mod intrinsics {
17+
#![unstable(feature = "unstable", issue = "none")]
18+
extern "rust-intrinsic" {
19+
#[rustc_const_stable(feature = "stable", since = "1.0.0")]
20+
pub fn transmute<T, U>(_: T) -> U; //~ ERROR functions cannot be const-stable if they are unstable
21+
}
22+
}
23+
1624
fn main() {}

src/test/ui/lint/incompatible_stability.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ error: functions cannot be const-stable if they are unstable
1212
LL | const fn foo() {}
1313
| ^^^^^^^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: functions cannot be const-stable if they are unstable
16+
--> $DIR/incompatible_stability.rs:20:9
17+
|
18+
LL | pub fn transmute<T, U>(_: T) -> U;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)