Skip to content

[flang] Disallow branches into SELECT TYPE/RANK cases #93893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions flang/lib/Semantics/resolve-labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class ParseTreeAnalyzer {
constructNames_.emplace_back(optionalName->ToString());
}
// Allow FORTRAN '66 extended DO ranges
PushScope().isExteriorGotoFatal = false;
PushScope(false);
// Process labels of the DO and END DO statements, but not the
// statements themselves, so that a non-construct END DO
// can be distinguished (below).
Expand All @@ -302,7 +302,7 @@ class ParseTreeAnalyzer {
bool Pre(const parser::IfConstruct &ifConstruct) {
return PushConstructName(ifConstruct);
}
void Post(const parser::IfThenStmt &) { PushScope(); }
void Post(const parser::IfThenStmt &) { PushScope(false); }
bool Pre(const parser::IfConstruct::ElseIfBlock &) {
return SwitchToNewScope();
}
Expand All @@ -316,19 +316,19 @@ class ParseTreeAnalyzer {
bool Pre(const parser::CaseConstruct &caseConstruct) {
return PushConstructName(caseConstruct);
}
void Post(const parser::SelectCaseStmt &) { PushScope(); }
void Post(const parser::SelectCaseStmt &) { PushScope(false); }
bool Pre(const parser::CaseConstruct::Case &) { return SwitchToNewScope(); }
bool Pre(const parser::SelectRankConstruct &selectRankConstruct) {
return PushConstructName(selectRankConstruct);
}
void Post(const parser::SelectRankStmt &) { PushScope(); }
void Post(const parser::SelectRankStmt &) { PushScope(true); }
bool Pre(const parser::SelectRankConstruct::RankCase &) {
return SwitchToNewScope();
}
bool Pre(const parser::SelectTypeConstruct &selectTypeConstruct) {
return PushConstructName(selectTypeConstruct);
}
void Post(const parser::SelectTypeStmt &) { PushScope(); }
void Post(const parser::SelectTypeStmt &) { PushScope(true); }
bool Pre(const parser::SelectTypeConstruct::TypeCase &) {
return SwitchToNewScope();
}
Expand Down Expand Up @@ -580,19 +580,20 @@ class ParseTreeAnalyzer {
SemanticsContext &ErrorHandler() { return context_; }

private:
ScopeInfo &PushScope() {
ScopeInfo &PushScope(bool isExteriorGotoFatal) {
auto &model{programUnits_.back().scopeModel};
int newDepth{model.empty() ? 1 : model[currentScope_].depth + 1};
ScopeInfo &result{model.emplace_back()};
result.parent = currentScope_;
result.depth = newDepth;
result.isExteriorGotoFatal = isExteriorGotoFatal;
currentScope_ = model.size() - 1;
return result;
}
bool InitializeNewScopeContext() {
programUnits_.emplace_back(UnitAnalysis{});
currentScope_ = 0u;
PushScope();
PushScope(false);
return true;
}
ScopeInfo &PopScope() {
Expand All @@ -604,9 +605,7 @@ class ParseTreeAnalyzer {
return programUnits_.back().scopeModel[currentScope_].parent;
}
bool SwitchToNewScope() {
ScopeInfo &oldScope{PopScope()};
bool isExteriorGotoFatal{oldScope.isExteriorGotoFatal};
PushScope().isExteriorGotoFatal = isExteriorGotoFatal;
PushScope(PopScope().isExteriorGotoFatal);
return true;
}

Expand All @@ -617,10 +616,9 @@ class ParseTreeAnalyzer {
}
// Gotos into this construct from outside it are diagnosed, and
// are fatal unless the construct is a DO, IF, or SELECT CASE.
PushScope().isExteriorGotoFatal =
!(std::is_same_v<A, parser::DoConstruct> ||
std::is_same_v<A, parser::IfConstruct> ||
std::is_same_v<A, parser::CaseConstruct>);
PushScope(!(std::is_same_v<A, parser::DoConstruct> ||
std::is_same_v<A, parser::IfConstruct> ||
std::is_same_v<A, parser::CaseConstruct>));
return true;
}
bool PushConstructName(const parser::BlockConstruct &blockConstruct) {
Expand All @@ -630,7 +628,7 @@ class ParseTreeAnalyzer {
if (optionalName) {
constructNames_.emplace_back(optionalName->ToString());
}
PushScope().isExteriorGotoFatal = true;
PushScope(true);
return true;
}
template <typename A> void PopConstructNameIfPresent(const A &a) {
Expand Down
32 changes: 32 additions & 0 deletions flang/test/Semantics/label05.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
! CHECK: error: Label '90' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '91' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '92' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '30' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '31' is in a construct that prevents its use as a branch target here
! CHECK-NOT: error: Label '32' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '40' is in a construct that prevents its use as a branch target here
! CHECK: error: Label '41' is in a construct that prevents its use as a branch target here
! CHECK-NOT: error: Label '42' is in a construct that prevents its use as a branch target here

subroutine sub00(a,b,n,m)
real a(n,m)
Expand Down Expand Up @@ -58,3 +64,29 @@ subroutine sub04(a,n)
end where
if (n - 3) 90, 91, 92
end subroutine sub04

subroutine sub05(a)
real a(..)
select rank (a)
rank(1)
31 goto 30
rank(2)
goto 32
32 continue
30 continue
end select
goto 31
end

subroutine sub06(a)
class(*) a
select type (a)
type is (integer)
41 goto 40
type is (real)
goto 42
42 continue
40 continue
end select
goto 41
end
Loading