use super::{assert_reported, assert_silent_about, codes_of};
use crate::DiagnosticCode;
#[test]
fn an_argument_that_does_not_satisfy_integral_is_reported() {
let codes = codes_of(
"template <std::integral T>\nT twice(T value) { return value + value; }\n\n\
int f() {\n twice(2);\n twice(\"text\");\n return 0;\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::ConceptNotSatisfied,
"`twice(\"text\")` passes a `const char*`",
);
}
#[test]
fn an_integer_is_not_a_floating_point() {
let wrong = codes_of(
"template <std::floating_point T>\nT half(T value) { return value; }\n\n\
int f() {\n half(1);\n return 0;\n}\n",
);
assert_reported(
&wrong,
DiagnosticCode::ConceptNotSatisfied,
"`int` is not `std::floating_point`",
);
let right = codes_of(
"template <std::floating_point T>\nT half(T value) { return value; }\n\n\
int f() {\n float x = 1.0f;\n half(x);\n return 0;\n}\n",
);
assert_silent_about(
&right,
DiagnosticCode::ConceptNotSatisfied,
"`float` is a floating-point type",
);
}
#[test]
fn a_class_that_does_not_derive_from_the_base_is_reported() {
let codes = codes_of(
"struct Base { virtual ~Base(); };\nstruct Derived : public Base { };\nstruct Plain { };\n\n\
template <std::derived_from<Base> T>\nvoid handle(T& value);\n\n\
void f(Derived d, Plain p, int n) {\n handle(d);\n handle(p);\n handle(n);\n}\n",
);
assert_reported(
&codes,
DiagnosticCode::ConceptNotSatisfied,
"`Plain` and `int` derive from nothing, while `Derived` derives from `Base`",
);
}
#[test]
fn a_user_written_concept_is_silent() {
let codes = codes_of(
"template <class T>\nconcept Small = sizeof(T) <= 4;\n\n\
template <Small T>\nT keep(T value) { return value; }\n\n\
int f() {\n keep(1.0);\n return 0;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::ConceptNotSatisfied,
"`Small` is not a concept this layer evaluates",
);
}
#[test]
fn an_overload_set_is_silent() {
let codes = codes_of(
"template <std::integral T>\nvoid pick(T value);\ntemplate <std::integral T>\nvoid pick(T value, int extra);\n\n\
void f() {\n pick(\"text\");\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::ConceptNotSatisfied,
"two declarations are an overload set",
);
}
#[test]
fn an_object_concept_over_a_value_type_is_silent() {
let codes = codes_of(
"template <std::copyable T>\nT copy_of(T value) { return value; }\n\n\
int f(int n) {\n copy_of(n);\n return 0;\n}\n",
);
assert_silent_about(
&codes,
DiagnosticCode::ConceptNotSatisfied,
"a value type is not answered about",
);
}