Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match const_context {
Some(hir::ConstContext::Const { .. } | hir::ConstContext::Static(_)) => {}
Some(hir::ConstContext::ConstFn) | None => {
self.dcx().span_err(span, "comptime fns can only be called at compile time");
self.dcx().emit_err(diagnostics::ComptimeFnCalledOutsideConstContext {
span,
const_block: diagnostics::WrapConstBlock {
left: span.shrink_to_lo(),
right: span.shrink_to_hi(),
},
});
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions compiler/rustc_hir_typeck/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,3 +1349,24 @@ impl ExprParenthesesNeeded {
#[derive(Diagnostic)]
#[diag("`#[panic_handler]` functions can't be `unsafe`")]
pub(crate) struct UnsafePanicHandlers;

#[derive(Diagnostic)]
#[diag("comptime functions can only be called at compile time")]
pub(crate) struct ComptimeFnCalledOutsideConstContext {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub const_block: WrapConstBlock,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
"wrap the call in a const block to force compile time evaluation",
applicability = "maybe-incorrect"
)]
pub(crate) struct WrapConstBlock {
#[suggestion_part(code = "const {{ ")]
pub left: Span,
#[suggestion_part(code = " }}")]
pub right: Span,
}
14 changes: 12 additions & 2 deletions tests/ui/attributes/doc_examples/rustc_comptime.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/rustc_comptime.rs:10:5
|
10 | comptime(); // ERROR
| ^^^^^^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
10 | const { comptime() }; // ERROR
| +++++++ +

error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/rustc_comptime.rs:18:5
|
18 | comptime();
| ^^^^^^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
18 | const { comptime() };
| +++++++ +

error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion tests/ui/comptime/comptime_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ const _: () = {

fn main() {
Bar.boo();
//~^ ERROR: comptime fns can only be called at compile time
//~^ ERROR: comptime functions can only be called at compile time
}
7 changes: 6 additions & 1 deletion tests/ui/comptime/comptime_method.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/comptime_method.rs:15:5
|
LL | Bar.boo();
| ^^^^^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
LL | const { Bar.boo() };
| +++++++ +

error: aborting due to 1 previous error

4 changes: 2 additions & 2 deletions tests/ui/comptime/not_callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ fn main() {
// Ok
const { foo() };
// Not Ok
foo(); //~ ERROR: comptime fns can only be called at compile time
foo(); //~ ERROR: comptime functions can only be called at compile time
}

const fn bar() {
// Not Ok
foo(); //~ ERROR: comptime fns can only be called at compile time
foo(); //~ ERROR: comptime functions can only be called at compile time
}

#[rustc_comptime]
Expand Down
14 changes: 12 additions & 2 deletions tests/ui/comptime/not_callable.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/not_callable.rs:10:5
|
LL | foo();
| ^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
LL | const { foo() };
| +++++++ +

error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/not_callable.rs:15:5
|
LL | foo();
| ^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
LL | const { foo() };
| +++++++ +

error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion tests/ui/reflection/reflection_methods_in_runtime_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ trait Trait {}
fn main() {
// Test the (lack of) usability of comptime fns in runtime code.
std::any::TypeId::of::<[u8; usize::MAX]>().trait_info_of::<dyn Trait>();
//~^ ERROR: comptime fns can only be called at compile time
//~^ ERROR: comptime functions can only be called at compile time
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
error: comptime fns can only be called at compile time
error: comptime functions can only be called at compile time
--> $DIR/reflection_methods_in_runtime_code.rs:7:5
|
LL | std::any::TypeId::of::<[u8; usize::MAX]>().trait_info_of::<dyn Trait>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: wrap the call in a const block to force compile time evaluation
|
LL | const { std::any::TypeId::of::<[u8; usize::MAX]>().trait_info_of::<dyn Trait>() };
| +++++++ +

error: aborting due to 1 previous error

Loading