See attached patch, 2/2 of the fix for PR127208. Without a coarray
library, FORM TEAM, CHANGE TEAM, END TEAM and SYNC TEAM were translated
to a call to exit (), so a program stopped silently at its first team
statement. With one image, a team is that image alone, so only team
numbers need tracking: the team variable holds the team number, and
two new libgfortran variables, _gfortran_current_team_number and
_gfortran_parent_team_number, hold the current and parent team numbers.
CHANGE TEAM saves them in temporaries and END TEAM restores them.

Start with trans-stmt.cc: the else branches of gfc_trans_form_team,
gfc_trans_change_team, gfc_trans_end_team and gfc_trans_sync_team,
which replace trans_exit (), and the helpers trans_team_stat_zero and
trans_team_number. current_team_save and parent_team_save are saved
and restored around the construct body, so nested constructs restore
their own pair. Then build_extern_int_var in trans-decl.cc, which
declares the two variables when no coarray library is used, and
conv_intrinsic_team_number and the new conv_intrinsic_get_team in
trans-intrinsic.cc, which read them. The one-line change in
gfc_match_allocate (match.cc) lets the existing
deallocate_allocated_coarrays deallocate coarrays allocated in the
construct at END TEAM in every coarray mode. gfc_simplify_get_team no
longer folds GET_TEAM to a null, so gfc_check_assign (expr.cc) drops
the exception that accepted it. The libgfortran part only defines and
exports the two variables. Of the tests, coarray_team_single_1 also
scans the tree dump for _gfortran_exit_i4, since a run test alone
passes against the bug; team_number_2 is new, and num_images_team_2 is
no longer skipped for -fcoarray=single.

The two variables are plain globals, as the coarray libraries keep
their current team too. An EXIT out of a CHANGE TEAM construct
still skips END TEAM, here as with -fcoarray=lib; that is a separate
bug.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR127208] Support the TEAM statements with -fcoarray=single

FORM TEAM, CHANGE TEAM, END TEAM and SYNC TEAM were translated to a
call to exit () when no coarray library is used, so a program using
teams silently stopped at its first team statement.  With a single
image a team consists of that image alone, so the statements only
have to keep track of the team number: the team variable holds the
number of the team it identifies, and the numbers of the current and
of the parent team are held in two libgfortran variables that the
generated code saves and restores across a CHANGE TEAM construct.
TEAM_NUMBER and GET_TEAM are translated inline from those variables.

Coarrays allocated inside a CHANGE TEAM construct are deallocated when
the team terminates (F2018 11.1.5.2).  The parser already builds the
guarded deallocations for that, in deallocate_allocated_coarrays, and
they do not depend on the coarray mode; only their collection in
gfc_match_allocate was restricted to -fcoarray=lib.

Since GET_TEAM is no longer simplified to a null expression, the
assignment check that accepted such a value can go too.

Assisted-by: Claude Opus 5.5

        PR fortran/127208

gcc/fortran/ChangeLog:

        * expr.cc (gfc_check_assign): Remove the exception for a null
        team_type value under -fcoarray=single.
        * match.cc (gfc_match_allocate): Collect coarrays allocated in a
        team construct without a coarray library too.
        * simplify.cc (gfc_simplify_get_team): Do not simplify for
        -fcoarray=single; the current team is only known at run time.
        * trans-decl.cc (gfor_gvar_current_team_number,
        gfor_gvar_parent_team_number): New variables.
        (build_extern_int_var): New function.
        (gfc_build_builtin_function_decls): Use it to declare the team
        number variables when no coarray library is used.
        * trans-intrinsic.cc (conv_intrinsic_get_team): New function.
        (conv_intrinsic_team_number): Without a coarray library, take the
        team number from the team variable, or for the current team from
        the libgfortran variable, instead of always returning -1.
        (gfc_conv_intrinsic_function): Call conv_intrinsic_get_team for
        GFC_ISYM_GET_TEAM without a coarray library.
        * trans-stmt.cc (current_team_save, parent_team_save): New
        variables.
        (trans_team_stat_zero, trans_team_number): New functions.
        (gfc_trans_form_team): Store the team number in the team
        variable instead of exiting.
        (gfc_trans_change_team): Save the team numbers and set the
        current one instead of exiting.
        (gfc_trans_end_team): Restore the saved team numbers instead of
        exiting.
        (gfc_trans_sync_team): Evaluate the team and set STAT= instead
        of exiting.
        * trans.h (gfor_gvar_current_team_number,
        gfor_gvar_parent_team_number): Declare.

gcc/testsuite/ChangeLog:

        * gfortran.dg/coarray/num_images_team_2.f90: Do not skip for
        -fcoarray=single.
        * gfortran.dg/coarray/team_number_2.f90: New test.
        * gfortran.dg/coarray_team_single_1.f90: New test.

libgfortran/ChangeLog:

        * gfortran.map (GFORTRAN_17): Add _gfortran_current_team_number
        and _gfortran_parent_team_number.
        * libgfortran.h (current_team_number, parent_team_number):
        Declare.
        * runtime/main.c (current_team_number, parent_team_number):
        Define, initialized to the initial team number -1.
>From 5282464313865d537de3ccb0a519b71fa5373767 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 12:29:38 -0800
Subject: [PATCH] fortran: [PR127208] Support the TEAM statements with
 -fcoarray=single

FORM TEAM, CHANGE TEAM, END TEAM and SYNC TEAM were translated to a
call to exit () when no coarray library is used, so a program using
teams silently stopped at its first team statement.  With a single
image a team consists of that image alone, so the statements only
have to keep track of the team number: the team variable holds the
number of the team it identifies, and the numbers of the current and
of the parent team are held in two libgfortran variables that the
generated code saves and restores across a CHANGE TEAM construct.
TEAM_NUMBER and GET_TEAM are translated inline from those variables.

Coarrays allocated inside a CHANGE TEAM construct are deallocated when
the team terminates (F2018 11.1.5.2).  The parser already builds the
guarded deallocations for that, in deallocate_allocated_coarrays, and
they do not depend on the coarray mode; only their collection in
gfc_match_allocate was restricted to -fcoarray=lib.

Since GET_TEAM is no longer simplified to a null expression, the
assignment check that accepted such a value can go too.

Assisted-by: Claude Opus 5.5

	PR fortran/127208

gcc/fortran/ChangeLog:

	* expr.cc (gfc_check_assign): Remove the exception for a null
	team_type value under -fcoarray=single.
	* match.cc (gfc_match_allocate): Collect coarrays allocated in a
	team construct without a coarray library too.
	* simplify.cc (gfc_simplify_get_team): Do not simplify for
	-fcoarray=single; the current team is only known at run time.
	* trans-decl.cc (gfor_gvar_current_team_number,
	gfor_gvar_parent_team_number): New variables.
	(build_extern_int_var): New function.
	(gfc_build_builtin_function_decls): Use it to declare the team
	number variables when no coarray library is used.
	* trans-intrinsic.cc (conv_intrinsic_get_team): New function.
	(conv_intrinsic_team_number): Without a coarray library, take the
	team number from the team variable, or for the current team from
	the libgfortran variable, instead of always returning -1.
	(gfc_conv_intrinsic_function): Call conv_intrinsic_get_team for
	GFC_ISYM_GET_TEAM without a coarray library.
	* trans-stmt.cc (current_team_save, parent_team_save): New
	variables.
	(trans_team_stat_zero, trans_team_number): New functions.
	(gfc_trans_form_team): Store the team number in the team
	variable instead of exiting.
	(gfc_trans_change_team): Save the team numbers and set the
	current one instead of exiting.
	(gfc_trans_end_team): Restore the saved team numbers instead of
	exiting.
	(gfc_trans_sync_team): Evaluate the team and set STAT= instead
	of exiting.
	* trans.h (gfor_gvar_current_team_number,
	gfor_gvar_parent_team_number): Declare.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/num_images_team_2.f90: Do not skip for
	-fcoarray=single.
	* gfortran.dg/coarray/team_number_2.f90: New test.
	* gfortran.dg/coarray_team_single_1.f90: New test.

libgfortran/ChangeLog:

	* gfortran.map (GFORTRAN_17): Add _gfortran_current_team_number
	and _gfortran_parent_team_number.
	* libgfortran.h (current_team_number, parent_team_number):
	Declare.
	* runtime/main.c (current_team_number, parent_team_number):
	Define, initialized to the initial team number -1.
---
 gcc/fortran/expr.cc                           |   8 +-
 gcc/fortran/match.cc                          |   2 +-
 gcc/fortran/simplify.cc                       |  14 +-
 gcc/fortran/trans-decl.cc                     |  29 +++++
 gcc/fortran/trans-intrinsic.cc                |  63 ++++++++-
 gcc/fortran/trans-stmt.cc                     | 122 +++++++++++++++++-
 gcc/fortran/trans.h                           |   5 +
 .../gfortran.dg/coarray/num_images_team_2.f90 |   1 -
 .../gfortran.dg/coarray/team_number_2.f90     |  49 +++++++
 .../gfortran.dg/coarray_team_single_1.f90     |  25 ++++
 libgfortran/gfortran.map                      |   2 +
 libgfortran/libgfortran.h                     |   9 ++
 libgfortran/runtime/main.c                    |  10 ++
 13 files changed, 307 insertions(+), 32 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/team_number_2.f90
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_team_single_1.f90

diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index 67c9aa6ac6f..291adb7791a 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -4144,13 +4144,7 @@ gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
       if (has_pointer && (ref == NULL || ref->next == NULL)
 	  && lvalue->symtree->n.sym->attr.data)
         return true;
-      /* Prevent the following error message for caf-single mode, because there
-	 are no teams in single mode and the simplify returns a null then.  */
-      else if (!(flag_coarray == GFC_FCOARRAY_SINGLE
-		 && rvalue->ts.type == BT_DERIVED
-		 && rvalue->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
-		 && rvalue->ts.u.derived->intmod_sym_id
-		      == ISOFORTRAN_TEAM_TYPE))
+      else
 	{
 	  gfc_error ("NULL appears on right-hand side in assignment at %L",
 		     &rvalue->where);
diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index df7aa685403..efa0b67d86c 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -5470,7 +5470,7 @@ gfc_match_allocate (void)
 	  codim = r->u.c.component->attr.codimension
 		  || (r->u.c.component->as && r->u.c.component->as->corank);
 
-      if (flag_coarray == GFC_FCOARRAY_LIB && team_ns && codim)
+      if (team_ns && codim)
 	{
 	  gfc_expr *e = gfc_copy_expr (tail->expr);
 	  vec<gfc_expr *> &allocated = team_allocated_coarrays.get_or_insert (team_ns);
diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index f334dafed1c..b2f3aa6cc15 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3421,18 +3421,8 @@ gfc_simplify_get_team (gfc_expr *level ATTRIBUTE_UNUSED)
       return &gfc_bad_expr;
     }
 
-  if (flag_coarray == GFC_FCOARRAY_SINGLE)
-    {
-      gfc_expr *result;
-      result = gfc_get_null_expr (&gfc_current_locus);
-      result->ts.type = BT_DERIVED;
-      gfc_find_symbol ("team_type", gfc_current_ns, 1, &result->ts.u.derived);
-
-      return result;
-    }
-
-  /* For fcoarray = lib no simplification is possible, because it is not known
-     what images failed or are stopped at compile time.  */
+  /* No simplification is possible: the current team is only known at run
+     time, with and without a coarray library.  */
   return NULL;
 }
 
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 5129637165a..e27b19de043 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -176,6 +176,9 @@ tree gfor_fndecl_co_sum;
 tree gfor_fndecl_caf_is_present_on_remote;
 tree gfor_fndecl_caf_random_init;
 
+tree gfor_gvar_current_team_number;
+tree gfor_gvar_parent_team_number;
+
 
 /* Math functions.  Many other math functions are handled in
    trans-intrinsic.cc.  */
@@ -4175,6 +4178,24 @@ gfc_build_intrinsic_function_decls (void)
 }
 
 
+/* Build a declaration for an integer variable defined in libgfortran.  */
+
+static tree
+build_extern_int_var (const char *name)
+{
+  tree decl = build_decl (input_location, VAR_DECL, get_identifier (name),
+			  integer_type_node);
+  DECL_EXTERNAL (decl) = 1;
+  TREE_PUBLIC (decl) = 1;
+  TREE_USED (decl) = 1;
+  DECL_ARTIFICIAL (decl) = 1;
+  DECL_IGNORED_P (decl) = 1;
+  pushdecl (decl);
+  rest_of_decl_compilation (decl, 1, 0);
+  return decl;
+}
+
+
 /* Make prototypes for runtime library functions.  */
 
 void
@@ -4551,6 +4572,14 @@ gfc_build_builtin_function_decls (void)
 	    get_identifier (PREFIX("caf_random_init")),
 	    void_type_node, 2, logical_type_node, logical_type_node);
     }
+  else
+    {
+      /* Without a coarray library the team numbers are kept in libgfortran.  */
+      gfor_gvar_current_team_number
+	= build_extern_int_var (PREFIX ("current_team_number"));
+      gfor_gvar_parent_team_number
+	= build_extern_int_var (PREFIX ("parent_team_number"));
+    }
 
   gfc_build_intrinsic_function_decls ();
   gfc_build_intrinsic_lib_fndecls ();
diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc
index 8afa33cbc84..7a6b1bbe999 100644
--- a/gcc/fortran/trans-intrinsic.cc
+++ b/gcc/fortran/trans-intrinsic.cc
@@ -2142,6 +2142,49 @@ conv_intrinsic_image_status (gfc_se *se, gfc_expr *expr)
   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
 }
 
+/* Translate GET_TEAM () without a coarray library.  A team variable holds
+   the team number of the team it identifies.  */
+
+static void
+conv_intrinsic_get_team (gfc_se *se, gfc_expr *expr)
+{
+  gfc_expr *level
+    = expr->value.function.actual ? expr->value.function.actual->expr : NULL;
+  tree tmp;
+
+  if (!level)
+    tmp = gfor_gvar_current_team_number;
+  else
+    {
+      gfc_se argse;
+      tree lvl, is_parent, is_initial;
+
+      gfc_init_se (&argse, NULL);
+      gfc_conv_expr_val (&argse, level);
+      gfc_add_block_to_block (&se->pre, &argse.pre);
+      lvl = gfc_evaluate_now (fold_convert (integer_type_node, argse.expr),
+			      &se->pre);
+      gfc_add_block_to_block (&se->post, &argse.post);
+
+      is_parent
+	= fold_build2_loc (input_location, EQ_EXPR, logical_type_node, lvl,
+			   build_int_cst (integer_type_node,
+					  GFC_CAF_PARENT_TEAM));
+      is_initial
+	= fold_build2_loc (input_location, EQ_EXPR, logical_type_node, lvl,
+			   build_int_cst (integer_type_node,
+					  GFC_CAF_INITIAL_TEAM));
+      tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
+			     is_initial, integer_minus_one_node,
+			     gfor_gvar_current_team_number);
+      tmp = fold_build3_loc (input_location, COND_EXPR, integer_type_node,
+			     is_parent, gfor_gvar_parent_team_number, tmp);
+    }
+
+  se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
+}
+
+
 static void
 conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
 {
@@ -2153,15 +2196,15 @@ conv_intrinsic_team_number (gfc_se *se, gfc_expr *expr)
   args = XALLOCAVEC (tree, num_args);
   gfc_conv_intrinsic_function_args (se, expr, args, num_args);
 
-  if (flag_coarray == GFC_FCOARRAY_SINGLE)
-    /* Only the initial team exists, and its team number is -1.  */
-    tmp = build_int_cst (integer_type_node, -1);
-  else if (flag_coarray == GFC_FCOARRAY_LIB)
+  if (flag_coarray != GFC_FCOARRAY_LIB && expr->value.function.actual->expr)
+    /* A team variable holds the team number without a coarray library.  */
+    tmp = gfc_evaluate_now (args[0], &se->pre);
+  else if (flag_coarray != GFC_FCOARRAY_LIB)
+    tmp = gfor_gvar_current_team_number;
+  else
     tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_team_number, 1,
 			       expr->value.function.actual->expr
 			       ? args[0] : null_pointer_node);
-  else
-    gcc_unreachable ();
 
   se->expr = fold_convert (gfc_get_int_type (gfc_default_integer_kind), tmp);
 }
@@ -11960,6 +12003,13 @@ gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
       trans_num_images (se, expr);
       break;
 
+    case GFC_ISYM_GET_TEAM:
+      if (flag_coarray != GFC_FCOARRAY_LIB)
+	conv_intrinsic_get_team (se, expr);
+      else
+	gfc_conv_intrinsic_funcall (se, expr);
+      break;
+
     case GFC_ISYM_ACCESS:
     case GFC_ISYM_CHDIR:
     case GFC_ISYM_CHMOD:
@@ -11977,7 +12027,6 @@ gfc_conv_intrinsic_function (gfc_se * se, gfc_expr * expr)
     case GFC_ISYM_GETGID:
     case GFC_ISYM_GETPID:
     case GFC_ISYM_GETUID:
-    case GFC_ISYM_GET_TEAM:
     case GFC_ISYM_HOSTNM:
     case GFC_ISYM_IERRNO:
     case GFC_ISYM_IRAND:
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 9ec80771a1f..53300dbd2a8 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -857,6 +857,48 @@ gfc_trans_sync_stat (struct sync_stat *sync_stat, gfc_se *se, tree *stat,
     }
 }
 
+/* Team numbers saved by gfc_trans_change_team for its END TEAM, without a
+   coarray library.  Nested constructs save and restore their own pair.  */
+
+static tree current_team_save, parent_team_save;
+
+
+/* Set a STAT= variable to zero.  Without a coarray library none of the team
+   statements can fail.  */
+
+static void
+trans_team_stat_zero (stmtblock_t *block, struct sync_stat *sync_stat)
+{
+  gfc_se se;
+
+  if (!sync_stat->stat)
+    return;
+
+  gfc_init_se (&se, NULL);
+  gfc_conv_expr (&se, sync_stat->stat);
+  gfc_add_block_to_block (block, &se.pre);
+  gfc_add_modify (block, se.expr, build_zero_cst (TREE_TYPE (se.expr)));
+  gfc_add_block_to_block (block, &se.post);
+}
+
+
+/* Evaluate EXPR into an integer temporary.  */
+
+static tree
+trans_team_number (stmtblock_t *block, gfc_expr *expr)
+{
+  gfc_se se;
+  tree tmp;
+
+  gfc_init_se (&se, NULL);
+  gfc_conv_expr_val (&se, expr);
+  gfc_add_block_to_block (block, &se.pre);
+  tmp = gfc_evaluate_now (fold_convert (integer_type_node, se.expr), block);
+  gfc_add_block_to_block (block, &se.post);
+  return tmp;
+}
+
+
 /* Translate the FORM TEAM statement.  */
 
 tree
@@ -899,7 +941,31 @@ gfc_trans_form_team (gfc_code *code)
       return gfc_finish_block (&se.pre);
      }
   else
-    return trans_exit ();
+    {
+      /* With one image, the team variable holds just the team number.  */
+      stmtblock_t block;
+      gfc_se se;
+      tree team_id;
+
+      gfc_start_block (&block);
+
+      team_id = trans_team_number (&block, code->expr1);
+
+      gfc_init_se (&se, NULL);
+      gfc_conv_expr (&se, code->expr2);
+      gfc_add_block_to_block (&block, &se.pre);
+      gfc_add_modify (&block, se.expr,
+		      fold_convert (TREE_TYPE (se.expr), team_id));
+      gfc_add_block_to_block (&block, &se.post);
+
+      /* NEW_INDEX= can only be one for a single image, but evaluate it for
+	 its side effects.  */
+      if (code->expr3)
+	trans_team_number (&block, code->expr3);
+
+      trans_team_stat_zero (&block, &code->ext.sync_stat);
+      return gfc_finish_block (&block);
+    }
 }
 
 /* Translate the CHANGE TEAM statement.  */
@@ -932,7 +998,36 @@ gfc_trans_change_team (gfc_code *code)
       return gfc_finish_block (&block);
     }
   else
-    return trans_exit ();
+    {
+      stmtblock_t block;
+      tree outer_current, outer_parent, team_no;
+
+      gfc_start_block (&block);
+
+      team_no = trans_team_number (&block, code->expr1);
+
+      outer_current = current_team_save;
+      outer_parent = parent_team_save;
+      current_team_save = gfc_create_var (integer_type_node, "save_team");
+      parent_team_save = gfc_create_var (integer_type_node, "save_parent_team");
+
+      gfc_add_modify (&block, current_team_save, gfor_gvar_current_team_number);
+      gfc_add_modify (&block, parent_team_save, gfor_gvar_parent_team_number);
+      gfc_add_modify (&block, gfor_gvar_parent_team_number,
+		      gfor_gvar_current_team_number);
+      gfc_add_modify (&block, gfor_gvar_current_team_number, team_no);
+
+      trans_team_stat_zero (&block, &code->ext.block.sync_stat);
+
+      /* The END TEAM statement is translated as part of the construct and
+	 restores the team numbers saved above.  */
+      gfc_add_expr_to_block (&block, gfc_trans_block_construct (code));
+
+      current_team_save = outer_current;
+      parent_team_save = outer_parent;
+
+      return gfc_finish_block (&block);
+    }
 }
 
 /* Translate the END TEAM statement.  */
@@ -958,7 +1053,18 @@ gfc_trans_end_team (gfc_code *code)
       return gfc_finish_block (&se.pre);
     }
   else
-    return trans_exit ();
+    {
+      stmtblock_t block;
+
+      gfc_start_block (&block);
+
+      gcc_assert (current_team_save && parent_team_save);
+      gfc_add_modify (&block, gfor_gvar_current_team_number, current_team_save);
+      gfc_add_modify (&block, gfor_gvar_parent_team_number, parent_team_save);
+
+      trans_team_stat_zero (&block, &code->ext.sync_stat);
+      return gfc_finish_block (&block);
+    }
 }
 
 /* Translate the SYNC TEAM statement.  */
@@ -986,7 +1092,15 @@ gfc_trans_sync_team (gfc_code *code)
       return gfc_finish_block (&se.pre);
     }
   else
-    return trans_exit ();
+    {
+      /* A single image never waits for anybody else.  */
+      stmtblock_t block;
+
+      gfc_start_block (&block);
+      trans_team_number (&block, code->expr1);
+      trans_team_stat_zero (&block, &code->ext.sync_stat);
+      return gfc_finish_block (&block);
+    }
 }
 
 tree
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index 4579a80fe47..aba71ffffcf 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -933,6 +933,11 @@ extern GTY(()) tree gfor_fndecl_co_reduce;
 extern GTY(()) tree gfor_fndecl_co_sum;
 extern GTY(()) tree gfor_fndecl_caf_is_present_on_remote;
 
+/* Team numbers of the current and of the parent team, held in libgfortran
+   for -fcoarray=single and -fcoarray=none.  */
+extern GTY(()) tree gfor_gvar_current_team_number;
+extern GTY(()) tree gfor_gvar_parent_team_number;
+
 /* Math functions.  Many other math functions are handled in
    trans-intrinsic.cc.  */
 
diff --git a/gcc/testsuite/gfortran.dg/coarray/num_images_team_2.f90 b/gcc/testsuite/gfortran.dg/coarray/num_images_team_2.f90
index ea0013cd06a..3c24e158740 100644
--- a/gcc/testsuite/gfortran.dg/coarray/num_images_team_2.f90
+++ b/gcc/testsuite/gfortran.dg/coarray/num_images_team_2.f90
@@ -1,5 +1,4 @@
 ! { dg-do run }
-! { dg-skip-if "CHANGE TEAM needs a coarray library" { *-*-* } { "-fcoarray=single" } { "" } }
 !
 ! PR 126781
 !
diff --git a/gcc/testsuite/gfortran.dg/coarray/team_number_2.f90 b/gcc/testsuite/gfortran.dg/coarray/team_number_2.f90
new file mode 100644
index 00000000000..5700b1cd831
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/team_number_2.f90
@@ -0,0 +1,49 @@
+!{ dg-do run }
+!
+! PR 127208
+
+  use iso_fortran_env, only : team_type, initial_team, parent_team, &
+                              current_team
+  implicit none
+  type(team_type) :: team, subteam
+  integer :: istat = 42
+
+  if (team_number() /= -1) stop 1
+  if (team_number(get_team()) /= -1) stop 2
+  if (team_number(get_team(initial_team)) /= -1) stop 3
+  if (team_number(get_team(parent_team)) /= -1) stop 4
+
+  form team (12, team, stat=istat)
+  if (istat /= 0) stop 5
+  if (team_number() /= -1) stop 6
+
+  change team (team, stat=istat)
+    if (istat /= 0) stop 7
+    if (team_number() /= 12) stop 8
+    if (team_number(get_team()) /= 12) stop 9
+    if (team_number(get_team(current_team)) /= 12) stop 10
+    if (team_number(get_team(parent_team)) /= -1) stop 11
+    if (in_a_procedure() /= 12) stop 12
+
+    form team (3, subteam)
+    change team (subteam)
+      if (team_number() /= 3) stop 13
+      if (team_number(get_team(parent_team)) /= 12) stop 14
+      if (team_number(get_team(initial_team)) /= -1) stop 15
+    end team
+
+    if (team_number() /= 12) stop 16
+    sync team (team, stat=istat)
+    if (istat /= 0) stop 17
+  end team (stat=istat)
+
+  if (istat /= 0) stop 18
+  if (team_number() /= -1) stop 19
+
+contains
+
+  integer function in_a_procedure ()
+    in_a_procedure = team_number ()
+  end function
+
+end
diff --git a/gcc/testsuite/gfortran.dg/coarray_team_single_1.f90 b/gcc/testsuite/gfortran.dg/coarray_team_single_1.f90
new file mode 100644
index 00000000000..855abf4d738
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_team_single_1.f90
@@ -0,0 +1,25 @@
+! { dg-do run }
+! { dg-options "-fcoarray=single -fdump-tree-original" }
+!
+! PR 127208
+
+program coarray_team_single_1
+  use iso_fortran_env, only : team_type
+  implicit none
+  type(team_type) :: t
+  integer :: st, flag
+
+  flag = 0
+  form team (1, t, stat=st)
+  if (st /= 0) stop 1
+  change team (t, stat=st)
+    if (st /= 0) stop 2
+    flag = 1
+    sync team (t, stat=st)
+    if (st /= 0) stop 3
+  end team (stat=st)
+  if (st /= 0) stop 4
+  if (flag /= 1) stop 5
+end program coarray_team_single_1
+
+! { dg-final { scan-tree-dump-not "_gfortran_exit_i4" "original" } }
diff --git a/libgfortran/gfortran.map b/libgfortran/gfortran.map
index 6e04842e953..514156fd51a 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -2042,6 +2042,8 @@ GFORTRAN_16 {
 
 GFORTRAN_17 {
   global:
+    _gfortran_current_team_number;
+    _gfortran_parent_team_number;
     _gfortran_specific__acosd_r10;
     _gfortran_specific__acosd_r16;
     _gfortran_specific__acosd_r17;
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index 3613b5d68c0..b2c725d1c8c 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -709,6 +709,15 @@ iexport_data_proto(line);
 extern char *filename;
 iexport_data_proto(filename);
 
+/* Team numbers of the current and of the parent team, maintained by code
+   generated for -fcoarray=single and -fcoarray=none.  */
+
+extern int current_team_number;
+iexport_data_proto(current_team_number);
+
+extern int parent_team_number;
+iexport_data_proto(parent_team_number);
+
 
 #define CHARACTER2(name) \
               gfc_charlen_type name ## _len; \
diff --git a/libgfortran/runtime/main.c b/libgfortran/runtime/main.c
index f90f2205628..77972f3c84d 100644
--- a/libgfortran/runtime/main.c
+++ b/libgfortran/runtime/main.c
@@ -38,6 +38,16 @@ static int argc_save;
 static char **argv_save;
 
 
+/* Team numbers of the current and the parent team without a coarray
+   library; the initial team is -1 (F2023 16.9.207).  */
+
+int current_team_number = -1;
+iexport_data(current_team_number);
+
+int parent_team_number = -1;
+iexport_data(parent_team_number);
+
+
 /* Set the saved values of the command line arguments.  */
 
 void
-- 
2.56.0

Reply via email to