Report a specific error when a type is bound to itself.
The generic error (I is not a base of C) is not very helpful in this case, see the discussion in #10.
diff --git a/include/fruit/impl/component_functors.defn.h b/include/fruit/impl/component_functors.defn.h
index d23e63b..57eb9b0 100644
--- a/include/fruit/impl/component_functors.defn.h
+++ b/include/fruit/impl/component_functors.defn.h
@@ -152,9 +152,11 @@
};
using I = RemoveAnnotations(AnnotatedI);
using C = RemoveAnnotations(AnnotatedC);
- using type = If(Not(IsBaseOf(I, C)),
+ using type = If(IsSame(I, C),
+ ConstructError(InterfaceBindingToSelfErrorTag, C),
+ If(Not(IsBaseOf(I, C)),
ConstructError(NotABaseClassOfErrorTag, I, C),
- Op);
+ Op));
};
};
diff --git a/include/fruit/impl/injection_errors.h b/include/fruit/impl/injection_errors.h
index 5a5949a..5e00556 100644
--- a/include/fruit/impl/injection_errors.h
+++ b/include/fruit/impl/injection_errors.h
@@ -256,6 +256,15 @@
"The specified class can't be constructed because it's an abstract class.");
};
+template <typename C>
+struct InterfaceBindingToSelfError {
+ static_assert(
+ AlwaysFalse<C>::value,
+ "The type C was bound to itself. If this was intentional, to \"tell Fruit to inject the type"
+ " C\", this binding is unnecessary, just remove it. bind<I,C>() is to tell Fruit about"
+ " base-derived class relationships.");
+};
+
struct LambdaWithCapturesErrorTag {
@@ -403,6 +412,10 @@
using apply = NoBindingFoundForAbstractClassError<C>;
};
+struct InterfaceBindingToSelfErrorTag {
+ template <typename C>
+ using apply = InterfaceBindingToSelfError<C>;
+};
} // namespace impl
} // namespace fruit
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 7a566f0..9bf9f6a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -3,6 +3,7 @@
set(TEST_SOURCES
binding_compression_undone.cpp
+binding_error_bound_to_itself.cpp
binding_error_not_base.cpp
binding_error_not_base_with_annotations.cpp
bind_instance_error_already_bound.cpp
diff --git a/tests/binding_error_bound_to_itself.cpp b/tests/binding_error_bound_to_itself.cpp
new file mode 100644
index 0000000..9571f8c
--- /dev/null
+++ b/tests/binding_error_bound_to_itself.cpp
@@ -0,0 +1,30 @@
+// expect-compile-error InterfaceBindingToSelfError<X>|The type C was bound to itself.
+/*
+ * Copyright 2014 Google Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fruit/fruit.h>
+#include "test_macros.h"
+
+using fruit::Component;
+using fruit::Injector;
+
+struct X {
+};
+
+Component<int> getComponent() {
+ return fruit::createComponent()
+ .bind<X, X>();
+}