From f450a6d74cd50ce0c1adaa0ae53c43f9e2014d70 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <bkchr@users.noreply.github.com>
Date: Wed, 21 Oct 2020 23:50:07 +0200
Subject: [PATCH] Make `decl_runtime_apis!` fail on methods with default
 implementation (#7371)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Make `decl_runtime_apis!` fail on methods with default implementation

Runtime api functions are not allowed to have default implementations.
This fixes this by throwing an error when we detect such a function.

* Update primitives/api/proc-macro/src/decl_runtime_apis.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update primitives/api/test/tests/ui/no_default_implementation.stderr

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
---
 .../primitives/api/proc-macro/src/decl_runtime_apis.rs   | 7 +++++++
 .../api/test/tests/ui/no_default_implementation.rs       | 9 +++++++++
 .../api/test/tests/ui/no_default_implementation.stderr   | 8 ++++++++
 3 files changed, 24 insertions(+)
 create mode 100644 substrate/primitives/api/test/tests/ui/no_default_implementation.rs
 create mode 100644 substrate/primitives/api/test/tests/ui/no_default_implementation.stderr

diff --git a/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs b/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs
index 8294c8bfbd6..a628ade6f9b 100644
--- a/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs
+++ b/substrate/primitives/api/proc-macro/src/decl_runtime_apis.rs
@@ -912,6 +912,13 @@ impl CheckTraitDecl {
 				.entry(method.sig.ident.clone())
 				.or_default()
 				.push(changed_in);
+
+			if method.default.is_some() {
+				self.errors.push(Error::new(
+					method.default.span(),
+					"A runtime API function cannot have a default implementation!",
+				));
+			}
 		});
 
 		method_to_signature_changed.into_iter().for_each(|(f, changed)| {
diff --git a/substrate/primitives/api/test/tests/ui/no_default_implementation.rs b/substrate/primitives/api/test/tests/ui/no_default_implementation.rs
new file mode 100644
index 00000000000..6af93d6b865
--- /dev/null
+++ b/substrate/primitives/api/test/tests/ui/no_default_implementation.rs
@@ -0,0 +1,9 @@
+sp_api::decl_runtime_apis! {
+	pub trait Api {
+		fn test() {
+			println!("Hey, I'm a default implementation!");
+		}
+	}
+}
+
+fn main() {}
diff --git a/substrate/primitives/api/test/tests/ui/no_default_implementation.stderr b/substrate/primitives/api/test/tests/ui/no_default_implementation.stderr
new file mode 100644
index 00000000000..0ccece14419
--- /dev/null
+++ b/substrate/primitives/api/test/tests/ui/no_default_implementation.stderr
@@ -0,0 +1,8 @@
+error: A runtime API function cannot have a default implementation!
+ --> $DIR/no_default_implementation.rs:3:13
+  |
+3 |           fn test() {
+  |  ___________________^
+4 | |             println!("Hey, I'm a default implementation!");
+5 | |         }
+  | |_________^
-- 
GitLab