mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 09:06:21 +01:00
Fix poor dyn_cast usage (#2763)
Note the dest_class case didn't actually check dyn_cast success until after using the pointer. That's the case I noticed first, but also cleaning up a couple other small related things.
This commit is contained in:
@@ -22,7 +22,7 @@ auto NamedElement::name() const -> std::string_view {
|
||||
if (const auto* decl = element_.dyn_cast<const Declaration*>()) {
|
||||
return GetName(*decl).value();
|
||||
} else {
|
||||
const auto* named_value = element_.dyn_cast<const NamedValue*>();
|
||||
const auto* named_value = element_.get<const NamedValue*>();
|
||||
return named_value->name;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ auto NamedElement::type() const -> const Value& {
|
||||
if (const auto* decl = element_.dyn_cast<const Declaration*>()) {
|
||||
return decl->static_type();
|
||||
} else {
|
||||
const auto* named_value = element_.dyn_cast<const NamedValue*>();
|
||||
const auto* named_value = element_.get<const NamedValue*>();
|
||||
return *named_value->value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -979,7 +979,7 @@ auto Interpreter::CallFunction(const CallExpression& call,
|
||||
}
|
||||
case Value::Kind::FunctionValue:
|
||||
case Value::Kind::BoundMethodValue: {
|
||||
const auto* func_val = dyn_cast<FunctionOrMethodValue>(fun);
|
||||
const auto* func_val = cast<FunctionOrMethodValue>(fun);
|
||||
|
||||
const FunctionDeclaration& function = func_val->declaration();
|
||||
if (!function.body().has_value()) {
|
||||
|
||||
@@ -711,12 +711,9 @@ auto TypeChecker::BuildSubtypeConversion(Nonnull<Expression*> source,
|
||||
Nonnull<const PointerType*> src_ptr,
|
||||
Nonnull<const PointerType*> dest_ptr)
|
||||
-> ErrorOr<Nonnull<const Expression*>> {
|
||||
const auto* src_class = dyn_cast<NominalClassType>(&src_ptr->pointee_type());
|
||||
const auto* dest_class =
|
||||
dyn_cast<NominalClassType>(&dest_ptr->pointee_type());
|
||||
const auto* src_class = cast<NominalClassType>(&src_ptr->pointee_type());
|
||||
const auto* dest_class = cast<NominalClassType>(&dest_ptr->pointee_type());
|
||||
const auto dest = dest_class->declaration().name();
|
||||
CARBON_CHECK(src_class && dest_class)
|
||||
<< "Invalid source or destination pointee";
|
||||
Nonnull<Expression*> last_expr = source;
|
||||
const auto* cur_class = src_class;
|
||||
while (!TypeEqual(cur_class, dest_class, std::nullopt)) {
|
||||
@@ -6040,9 +6037,7 @@ auto TypeChecker::TypeCheckDeclaration(
|
||||
if (var.has_initializer()) {
|
||||
CARBON_RETURN_IF_ERROR(TypeCheckExp(&var.initializer(), impl_scope));
|
||||
}
|
||||
const auto* binding_type =
|
||||
dyn_cast<ExpressionPattern>(&var.binding().type());
|
||||
if (binding_type == nullptr) {
|
||||
if (!isa<ExpressionPattern>(&var.binding().type())) {
|
||||
// TODO: consider adding support for `auto`
|
||||
return ProgramError(var.source_loc())
|
||||
<< "Type of a top-level variable must be an expression.";
|
||||
@@ -6153,7 +6148,7 @@ auto TypeChecker::DeclareDeclaration(Nonnull<Declaration*> d,
|
||||
auto& var = cast<VariableDeclaration>(*d);
|
||||
// Associate the variable name with it's declared type in the
|
||||
// compile-time symbol table.
|
||||
if (!llvm::isa<ExpressionPattern>(var.binding().type())) {
|
||||
if (!isa<ExpressionPattern>(var.binding().type())) {
|
||||
return ProgramError(var.binding().type().source_loc())
|
||||
<< "Expected expression for variable type";
|
||||
}
|
||||
@@ -6213,7 +6208,7 @@ auto TypeChecker::FindMixedMemberAndType(
|
||||
-> ErrorOr<std::optional<
|
||||
std::pair<Nonnull<const Value*>, Nonnull<const Declaration*>>>> {
|
||||
for (Nonnull<const Declaration*> member : members) {
|
||||
if (llvm::isa<MixDeclaration>(member)) {
|
||||
if (isa<MixDeclaration>(member)) {
|
||||
const auto& mix_decl = cast<MixDeclaration>(*member);
|
||||
Nonnull<const MixinPseudoType*> mixin = &mix_decl.mixin_value();
|
||||
CARBON_ASSIGN_OR_RETURN(
|
||||
|
||||
Reference in New Issue
Block a user