[flang-commits] [flang] 539a6b5 - [flang] Detect use-before-decl errors on type parameters (#99947)
via flang-commits
flang-commits at lists.llvm.org
Tue Jul 30 09:42:18 PDT 2024
Author: Peter Klausler
Date: 2024-07-30T09:42:15-07:00
New Revision: 539a6b500c9c7a808e68db32bf7965985202197f
URL: https://github.com/llvm/llvm-project/commit/539a6b500c9c7a808e68db32bf7965985202197f
DIFF: https://github.com/llvm/llvm-project/commit/539a6b500c9c7a808e68db32bf7965985202197f.diff
LOG: [flang] Detect use-before-decl errors on type parameters (#99947)
Ensure that type parameters are declared as such before being referenced
within the derived type definition. (Previously, such references would
resolve to symbols in the enclosing scope.)
This change causes the symbols for the type parameters to be created
when the TYPE statement is processed in name resolution. They are
TypeParamDetails symbols with no KIND/LEN attribute set, and they shadow
any symbols of the same name in the enclosing scope.
When the type parameter declarations are processed, the KIND/LEN
attributes are set. Any earlier reference to a type parameter with no
KIND/LEN attribute elicits an error.
Some members of TypeParamDetails have been retyped &/or renamed.
Added:
Modified:
flang/include/flang/Semantics/symbol.h
flang/include/flang/Semantics/tools.h
flang/lib/Semantics/check-declarations.cpp
flang/lib/Semantics/mod-file.cpp
flang/lib/Semantics/resolve-names.cpp
flang/lib/Semantics/runtime-type-info.cpp
flang/lib/Semantics/symbol.cpp
flang/lib/Semantics/tools.cpp
flang/lib/Semantics/type.cpp
flang/test/Semantics/resolve33.f90
Removed:
################################################################################
diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index cdbe3e39386bb..cf0350735b5b9 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -460,15 +460,19 @@ class ProcEntityDetails : public EntityDetails, public WithPassArg {
// and specialized for each distinct set of type parameter values.
class DerivedTypeDetails {
public:
- const std::list<SourceName> ¶mNames() const { return paramNames_; }
- const SymbolVector ¶mDecls() const { return paramDecls_; }
+ const SymbolVector ¶mNameOrder() const { return paramNameOrder_; }
+ const SymbolVector ¶mDeclOrder() const { return paramDeclOrder_; }
bool sequence() const { return sequence_; }
bool isDECStructure() const { return isDECStructure_; }
std::map<SourceName, SymbolRef> &finals() { return finals_; }
const std::map<SourceName, SymbolRef> &finals() const { return finals_; }
bool isForwardReferenced() const { return isForwardReferenced_; }
- void add_paramName(const SourceName &name) { paramNames_.push_back(name); }
- void add_paramDecl(const Symbol &symbol) { paramDecls_.push_back(symbol); }
+ void add_paramNameOrder(const Symbol &symbol) {
+ paramNameOrder_.push_back(symbol);
+ }
+ void add_paramDeclOrder(const Symbol &symbol) {
+ paramDeclOrder_.push_back(symbol);
+ }
void add_component(const Symbol &);
void set_sequence(bool x = true) { sequence_ = x; }
void set_isDECStructure(bool x = true) { isDECStructure_ = x; }
@@ -491,12 +495,12 @@ class DerivedTypeDetails {
const Symbol *GetFinalForRank(int) const;
private:
- // These are (1) the names of the derived type parameters in the order
+ // These are (1) the symbols of the derived type parameters in the order
// in which they appear on the type definition statement(s), and (2) the
// symbols that correspond to those names in the order in which their
// declarations appear in the derived type definition(s).
- std::list<SourceName> paramNames_;
- SymbolVector paramDecls_;
+ SymbolVector paramNameOrder_;
+ SymbolVector paramDeclOrder_;
// These are the names of the derived type's components in component
// order. A parent component, if any, appears first in this list.
std::list<SourceName> componentNames_;
@@ -565,18 +569,19 @@ class MiscDetails {
class TypeParamDetails {
public:
- explicit TypeParamDetails(common::TypeParamAttr attr) : attr_{attr} {}
+ TypeParamDetails() = default;
TypeParamDetails(const TypeParamDetails &) = default;
- common::TypeParamAttr attr() const { return attr_; }
+ std::optional<common::TypeParamAttr> attr() const { return attr_; }
+ TypeParamDetails &set_attr(common::TypeParamAttr);
MaybeIntExpr &init() { return init_; }
const MaybeIntExpr &init() const { return init_; }
void set_init(MaybeIntExpr &&expr) { init_ = std::move(expr); }
const DeclTypeSpec *type() const { return type_; }
- void set_type(const DeclTypeSpec &);
+ TypeParamDetails &set_type(const DeclTypeSpec &);
void ReplaceType(const DeclTypeSpec &);
private:
- common::TypeParamAttr attr_;
+ std::optional<common::TypeParamAttr> attr_;
MaybeIntExpr init_;
const DeclTypeSpec *type_{nullptr};
};
diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index ae0b79a6e4849..0aee1ef299bc8 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -260,7 +260,7 @@ std::optional<parser::MessageFixedText> GetImageControlStmtCoarrayMsg(
SymbolVector OrderParameterDeclarations(const Symbol &);
// Returns the complete list of derived type parameter names in the
// order defined by 7.5.3.2.
-std::list<SourceName> OrderParameterNames(const Symbol &);
+SymbolVector OrderParameterNames(const Symbol &);
// Return an existing or new derived type instance
const DeclTypeSpec &FindOrInstantiateDerivedType(Scope &, DerivedTypeSpec &&,
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index eb898bad53a4f..a52f013a70b9d 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -2919,7 +2919,7 @@ parser::Messages CheckHelper::WhyNotInteroperableDerivedType(
if (derived->sequence()) { // C1801
msgs.Say(symbol.name(),
"An interoperable derived type cannot have the SEQUENCE attribute"_err_en_US);
- } else if (!derived->paramDecls().empty()) { // C1802
+ } else if (!derived->paramNameOrder().empty()) { // C1802
msgs.Say(symbol.name(),
"An interoperable derived type cannot have a type parameter"_err_en_US);
} else if (const auto *parent{
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index a1c4c031ca509..c7e7716a7b481 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -410,15 +410,14 @@ bool ModFileWriter::PutComponents(const Symbol &typeSymbol) {
llvm::raw_string_ostream typeBindings{buf};
UnorderedSymbolSet emitted;
SymbolVector symbols{scope.GetSymbols()};
- // Emit type parameters first
- for (const Symbol &symbol : symbols) {
- if (symbol.has<TypeParamDetails>()) {
- PutSymbol(typeBindings, symbol);
- emitted.emplace(symbol);
- }
- }
- // Emit components in component order.
+ // Emit type parameter declarations first, in order
const auto &details{typeSymbol.get<DerivedTypeDetails>()};
+ for (const Symbol &symbol : details.paramDeclOrder()) {
+ CHECK(symbol.has<TypeParamDetails>());
+ PutSymbol(typeBindings, symbol);
+ emitted.emplace(symbol);
+ }
+ // Emit actual components in component order.
for (SourceName name : details.componentNames()) {
auto iter{scope.find(name)};
if (iter != scope.end()) {
@@ -549,10 +548,10 @@ void ModFileWriter::PutDerivedType(
decls_ << ",extends(" << extends->name() << ')';
}
decls_ << "::" << typeSymbol.name();
- if (!details.paramNames().empty()) {
+ if (!details.paramNameOrder().empty()) {
char sep{'('};
- for (const auto &name : details.paramNames()) {
- decls_ << sep << name;
+ for (const SymbolRef &ref : details.paramNameOrder()) {
+ decls_ << sep << ref->name();
sep = ',';
}
decls_ << ')';
@@ -1046,7 +1045,7 @@ void ModFileWriter::PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
os, symbol,
[&]() {
PutType(os, DEREF(symbol.GetType()));
- PutLower(os << ',', common::EnumToString(details.attr()));
+ PutLower(os << ',', common::EnumToString(details.attr().value()));
},
symbol.attrs());
PutInit(os, details.init());
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index f761355d0da32..95d021e3aa647 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -5456,34 +5456,14 @@ bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
CHECK(scope.symbol());
CHECK(scope.symbol()->scope() == &scope);
auto &details{scope.symbol()->get<DerivedTypeDetails>()};
- std::set<SourceName> paramNames;
for (auto ¶mName : std::get<std::list<parser::Name>>(stmt.statement.t)) {
- details.add_paramName(paramName.source);
- auto *symbol{FindInScope(scope, paramName)};
- if (!symbol) {
- Say(paramName,
- "No definition found for type parameter '%s'"_err_en_US); // C742
- // No symbol for a type param. Create one and mark it as containing an
- // error to improve subsequent semantic processing
- BeginAttrs();
- Symbol *typeParam{MakeTypeSymbol(
- paramName, TypeParamDetails{common::TypeParamAttr::Len})};
- context().SetError(*typeParam);
- EndAttrs();
- } else if (!symbol->has<TypeParamDetails>()) {
- Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
- *symbol, "Definition of '%s'"_en_US); // C741
- }
- if (!paramNames.insert(paramName.source).second) {
- Say(paramName,
- "Duplicate type parameter name: '%s'"_err_en_US); // C731
- }
- }
- for (const auto &[name, symbol] : currScope()) {
- if (symbol->has<TypeParamDetails>() && !paramNames.count(name)) {
- SayDerivedType(name,
- "'%s' is not a type parameter of this derived type"_err_en_US,
- currScope()); // C741
+ if (auto *symbol{FindInScope(scope, paramName)}) {
+ if (auto *details{symbol->detailsIf<TypeParamDetails>()}) {
+ if (!details->attr()) {
+ Say(paramName,
+ "No definition found for type parameter '%s'"_err_en_US); // C742
+ }
+ }
}
}
Walk(std::get<std::list<parser::Statement<parser::PrivateOrSequence>>>(x.t));
@@ -5499,7 +5479,7 @@ bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
"A sequence type should have at least one component"_warn_en_US);
}
}
- if (!details.paramNames().empty()) { // C740
+ if (!details.paramDeclOrder().empty()) { // C740
Say(stmt.source,
"A sequence type may not have type parameters"_err_en_US);
}
@@ -5559,24 +5539,50 @@ void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
details.add_component(comp);
}
}
+ // Create symbols now for type parameters so that they shadow names
+ // from the enclosing specification part.
+ if (auto *details{symbol.detailsIf<DerivedTypeDetails>()}) {
+ for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
+ if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{})}) {
+ details->add_paramNameOrder(*symbol);
+ }
+ }
+ }
EndAttrs();
}
void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
auto *type{GetDeclTypeSpec()};
+ DerivedTypeDetails *derivedDetails{nullptr};
+ if (Symbol * dtSym{currScope().symbol()}) {
+ derivedDetails = dtSym->detailsIf<DerivedTypeDetails>();
+ }
auto attr{std::get<common::TypeParamAttr>(x.t)};
for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
auto &name{std::get<parser::Name>(decl.t)};
- if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{attr})}) {
- SetType(name, *type);
- if (auto &init{
- std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
- if (auto maybeExpr{AnalyzeExpr(context(), *init)}) {
- if (auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)}) {
- symbol->get<TypeParamDetails>().set_init(std::move(*intExpr));
+ if (Symbol * symbol{FindInScope(currScope(), name)}) {
+ if (auto *paramDetails{symbol->detailsIf<TypeParamDetails>()}) {
+ if (!paramDetails->attr()) {
+ paramDetails->set_attr(attr);
+ SetType(name, *type);
+ if (auto &init{std::get<std::optional<parser::ScalarIntConstantExpr>>(
+ decl.t)}) {
+ if (auto maybeExpr{AnalyzeExpr(context(), *init)}) {
+ if (auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)}) {
+ paramDetails->set_init(std::move(*intExpr));
+ }
+ }
+ }
+ if (derivedDetails) {
+ derivedDetails->add_paramDeclOrder(*symbol);
}
+ } else {
+ Say(name,
+ "Type parameter '%s' was already declared in this derived type"_err_en_US);
}
}
+ } else {
+ Say(name, "'%s' is not a parameter of this derived type"_err_en_US);
}
}
EndDecl();
@@ -6779,9 +6785,6 @@ Symbol *DeclarationVisitor::MakeTypeSymbol(
}
Symbol &result{MakeSymbol(name, attrs, std::move(details))};
SetCUDADataAttr(name, result, cudaDataAttr());
- if (result.has<TypeParamDetails>()) {
- derivedType.symbol()->get<DerivedTypeDetails>().add_paramDecl(result);
- }
return &result;
}
}
@@ -7830,6 +7833,12 @@ const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
CheckEntryDummyUse(name.source, symbol);
ConvertToObjectEntity(*symbol);
ApplyImplicitRules(*symbol);
+ } else if (const auto *tpd{symbol->detailsIf<TypeParamDetails>()};
+ tpd && !tpd->attr()) {
+ Say(name,
+ "Type parameter '%s' was referenced before being declared"_err_en_US,
+ name.source);
+ context().SetError(*symbol);
}
if (checkIndexUseInOwnBounds_ &&
*checkIndexUseInOwnBounds_ == name.source && !InModuleFile()) {
diff --git a/flang/lib/Semantics/runtime-type-info.cpp b/flang/lib/Semantics/runtime-type-info.cpp
index 8939dc4499ec4..6690924196673 100644
--- a/flang/lib/Semantics/runtime-type-info.cpp
+++ b/flang/lib/Semantics/runtime-type-info.cpp
@@ -42,7 +42,8 @@ static int FindLenParameterIndex(
if (&*ref == &symbol) {
return lenIndex;
}
- if (ref->get<TypeParamDetails>().attr() == common::TypeParamAttr::Len) {
+ if (auto attr{ref->get<TypeParamDetails>().attr()};
+ attr && *attr == common::TypeParamAttr::Len) {
++lenIndex;
}
}
@@ -371,7 +372,7 @@ static std::optional<std::string> GetSuffixIfTypeKindParameters(
std::optional<std::string> suffix;
for (SymbolRef ref : *parameters) {
const auto &tpd{ref->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Kind) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Kind) {
if (const auto *pv{derivedTypeSpec.FindParameter(ref->name())}) {
if (pv->GetExplicit()) {
if (auto instantiatedValue{evaluate::ToInt64(*pv->GetExplicit())}) {
@@ -497,7 +498,7 @@ const Symbol *RuntimeTableBuilder::DescribeType(Scope &dtScope) {
for (SymbolRef ref : *parameters) {
if (const auto *inst{dtScope.FindComponent(ref->name())}) {
const auto &tpd{inst->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Kind) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Kind) {
auto value{evaluate::ToInt64(tpd.init()).value_or(0)};
if (derivedTypeSpec) {
if (const auto *pv{derivedTypeSpec->FindParameter(inst->name())}) {
@@ -799,7 +800,7 @@ evaluate::StructureConstructor RuntimeTableBuilder::DescribeComponent(
specParams{GetTypeParameters(spec.typeSymbol())}) {
for (SymbolRef ref : *specParams) {
const auto &tpd{ref->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Len) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Len) {
if (const ParamValue *
paramValue{spec.FindParameter(ref->name())}) {
lenParams.emplace_back(GetValue(*paramValue, parameters));
diff --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp
index 023ab7b64e4fc..31e91ee7355e3 100644
--- a/flang/lib/Semantics/symbol.cpp
+++ b/flang/lib/Semantics/symbol.cpp
@@ -588,7 +588,11 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
},
[&](const TypeParamDetails &x) {
DumpOptional(os, "type", x.type());
- os << ' ' << common::EnumToString(x.attr());
+ if (auto attr{x.attr()}) {
+ os << ' ' << common::EnumToString(*attr);
+ } else {
+ os << " (no attr)";
+ }
DumpExpr(os, "init", x.init());
},
[&](const MiscDetails &x) {
@@ -739,9 +743,16 @@ const Symbol *DerivedTypeDetails::GetFinalForRank(int rank) const {
return nullptr;
}
-void TypeParamDetails::set_type(const DeclTypeSpec &type) {
+TypeParamDetails &TypeParamDetails::set_attr(common::TypeParamAttr attr) {
+ CHECK(!attr_);
+ attr_ = attr;
+ return *this;
+}
+
+TypeParamDetails &TypeParamDetails::set_type(const DeclTypeSpec &type) {
CHECK(!type_);
type_ = &type;
+ return *this;
}
bool GenericKind::IsIntrinsicOperator() const {
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index dfc6cba6181b9..fdaf052c2d34e 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -1135,12 +1135,12 @@ std::optional<parser::MessageFormattedText> CheckAccessibleSymbol(
return std::nullopt;
}
-std::list<SourceName> OrderParameterNames(const Symbol &typeSymbol) {
- std::list<SourceName> result;
+SymbolVector OrderParameterNames(const Symbol &typeSymbol) {
+ SymbolVector result;
if (const DerivedTypeSpec * spec{typeSymbol.GetParentTypeSpec()}) {
result = OrderParameterNames(spec->typeSymbol());
}
- const auto ¶mNames{typeSymbol.get<DerivedTypeDetails>().paramNames()};
+ const auto ¶mNames{typeSymbol.get<DerivedTypeDetails>().paramNameOrder()};
result.insert(result.end(), paramNames.begin(), paramNames.end());
return result;
}
@@ -1150,7 +1150,7 @@ SymbolVector OrderParameterDeclarations(const Symbol &typeSymbol) {
if (const DerivedTypeSpec * spec{typeSymbol.GetParentTypeSpec()}) {
result = OrderParameterDeclarations(spec->typeSymbol());
}
- const auto ¶mDecls{typeSymbol.get<DerivedTypeDetails>().paramDecls()};
+ const auto ¶mDecls{typeSymbol.get<DerivedTypeDetails>().paramDeclOrder()};
result.insert(result.end(), paramDecls.begin(), paramDecls.end());
return result;
}
diff --git a/flang/lib/Semantics/type.cpp b/flang/lib/Semantics/type.cpp
index ed2474377173f..cfaee0b8ba6dc 100644
--- a/flang/lib/Semantics/type.cpp
+++ b/flang/lib/Semantics/type.cpp
@@ -63,7 +63,6 @@ void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
// Parameters of the most deeply nested "base class" come first when the
// derived type is an extension.
auto parameterNames{OrderParameterNames(typeSymbol_)};
- auto parameterDecls{OrderParameterDeclarations(typeSymbol_)};
auto nextNameIter{parameterNames.begin()};
RawParameters raw{std::move(rawParameters_)};
for (auto &[maybeKeyword, value] : raw) {
@@ -71,25 +70,25 @@ void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
common::TypeParamAttr attr{common::TypeParamAttr::Kind};
if (maybeKeyword) {
name = maybeKeyword->v.source;
- auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
+ auto it{std::find_if(parameterNames.begin(), parameterNames.end(),
[&](const Symbol &symbol) { return symbol.name() == name; })};
- if (it == parameterDecls.end()) {
+ if (it == parameterNames.end()) {
messages.Say(name,
"'%s' is not the name of a parameter for derived type '%s'"_err_en_US,
name, typeSymbol_.name());
} else {
// Resolve the keyword's symbol
maybeKeyword->v.symbol = const_cast<Symbol *>(&it->get());
- attr = it->get().get<TypeParamDetails>().attr();
+ if (const auto *tpd{it->get().detailsIf<TypeParamDetails>()}) {
+ attr = tpd->attr().value_or(attr);
+ }
}
} else if (nextNameIter != parameterNames.end()) {
- name = *nextNameIter++;
- auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
- [&](const Symbol &symbol) { return symbol.name() == name; })};
- if (it == parameterDecls.end()) {
- break;
+ name = nextNameIter->get().name();
+ if (const auto *tpd{nextNameIter->get().detailsIf<TypeParamDetails>()}) {
+ attr = tpd->attr().value_or(attr);
}
- attr = it->get().get<TypeParamDetails>().attr();
+ ++nextNameIter;
} else {
messages.Say(name_,
"Too many type parameters given for derived type '%s'"_err_en_US,
@@ -160,7 +159,7 @@ void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
// Default type parameter value expressions are folded within
// the scope of the derived type being instantiated.
const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
- if (details.init()) {
+ if (details.init() && details.attr()) {
evaluate::DynamicType dyType{TypeCategory::Integer, parameterKind};
if (auto converted{
evaluate::ConvertToType(dyType, SomeExpr{*details.init()})}) {
@@ -169,8 +168,8 @@ void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
evaluate::Fold(foldingContext, std::move(*converted))};
ok = ok || evaluate::IsActuallyConstant(folded);
AddParamValue(name,
- ParamValue{
- std::move(std::get<SomeIntExpr>(folded.u)), details.attr()});
+ ParamValue{std::move(std::get<SomeIntExpr>(folded.u)),
+ details.attr().value()});
} else {
if (!context.HasError(symbol)) {
evaluate::SayWithDeclaration(messages, symbol,
@@ -233,7 +232,7 @@ ParamValue *DerivedTypeSpec::FindParameter(SourceName target) {
static bool MatchKindParams(const Symbol &typeSymbol,
const DerivedTypeSpec &thisSpec, const DerivedTypeSpec &thatSpec) {
- for (auto ref : typeSymbol.get<DerivedTypeDetails>().paramDecls()) {
+ for (auto ref : typeSymbol.get<DerivedTypeDetails>().paramNameOrder()) {
if (ref->get<TypeParamDetails>().attr() == common::TypeParamAttr::Kind) {
const auto *thisValue{thisSpec.FindParameter(ref->name())};
const auto *thatValue{thatSpec.FindParameter(ref->name())};
@@ -369,12 +368,15 @@ void DerivedTypeSpec::Instantiate(Scope &containingScope) {
// uninitialized type parameter to forestall use of any default.
if (ParamValue * paramValue{FindParameter(name)}) {
const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
- paramValue->set_attr(details.attr());
+ TypeParamDetails instanceDetails{};
+ if (details.attr()) {
+ paramValue->set_attr(*details.attr());
+ instanceDetails.set_attr(*details.attr());
+ }
desc += sep;
desc += name.ToString();
desc += '=';
sep = ',';
- TypeParamDetails instanceDetails{details.attr()};
if (MaybeIntExpr expr{paramValue->GetExplicit()}) {
desc += expr->AsFortran();
instanceDetails.set_init(
diff --git a/flang/test/Semantics/resolve33.f90 b/flang/test/Semantics/resolve33.f90
index 4b27a8d231f52..88ebd1be4637b 100644
--- a/flang/test/Semantics/resolve33.f90
+++ b/flang/test/Semantics/resolve33.f90
@@ -10,7 +10,7 @@
! in that derived-type-def.
module m
- !ERROR: Duplicate type parameter name: 'a'
+ !ERROR: Type parameter, component, or procedure binding 'a' already defined in this type
type t1(a, b, a)
integer, kind :: a
integer(8), len :: b
@@ -23,23 +23,31 @@ module m
!ERROR: No definition found for type parameter 'b'
type t3(a, b)
integer, kind :: a
+ !ERROR: Component 'b' is already declared in this derived type
integer :: b
end type
type t4(a)
integer, kind :: a
- !ERROR: 'd' is not a type parameter of this derived type
+ !ERROR: 'd' is not a parameter of this derived type
integer(8), len :: d
end type
type t5(a, b)
integer, len :: a
integer, len :: b
- !ERROR: Type parameter, component, or procedure binding 'a' already defined in this type
+ !ERROR: Type parameter 'a' was already declared in this derived type
integer, len :: a
end type
!ERROR: No definition found for type parameter 'k'
!ERROR: No definition found for type parameter 'l'
type :: t6(k, l)
+ !ERROR: Type parameter 'k' was referenced before being declared
+ !ERROR: Type parameter 'l' was referenced before being declared
character(kind=k, len=l) :: d3
end type
type(t6(2, 10)) :: x3
+ type :: t7(k1,k2)
+ !ERROR: Type parameter 'k2' was referenced before being declared
+ integer(kind(k2)), kind :: k1
+ integer(kind(k1)), kind :: k2
+ end type
end module
More information about the flang-commits
mailing list