https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49232
--- Comment #1 from Thomas Koenig <tkoenig at gcc dot gnu.org> --- Straightforward patch: Index: expr.c =================================================================== --- expr.c (Revision 239218) +++ expr.c (Arbeitskopie) @@ -3764,6 +3764,45 @@ } } + /* Warn for suspicious assignments like + + pointer, dimension(:), contiguous :: p + p => a(:,:,2) */ + + if (lhs_attr.contiguous) + { + gfc_array_ref *ar; + int i; + + ar = NULL; + + for (ref = rvalue->ref; ref; ref = ref->next) + { + if (ref->type == REF_ARRAY) + { + ar = &ref->u.ar; + break; + } + } + if (ar->type == AR_SECTION) + { + for (i = 0; i < ar->dimen; i++) + { + if (ar->dimen_type[i] == DIMEN_VECTOR) + gfc_warning (OPT_Wall, "Assignment to contiguous pointer from" + "vector-subscripted target at %L", &rvalue->where); + + if (ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] + && (ar->end[i]->expr_type != EXPR_CONSTANT + || (ar->end[i]->expr_type == EXPR_CONSTANT + && mpz_cmp_si (ar->end[i]->value.integer, 1) != 0))) + gfc_warning (OPT_Wall, "Assignment to contiguous pointer from " + "possibly non-contiguous target at %L", + &rvalue->where); + } + } + } + /* Warn if it is the LHS pointer may lives longer than the RHS target. */ if (warn_target_lifetime && rvalue->expr_type == EXPR_VARIABLE We probably don't want an unconditional error for stuff p => a(:,:,2) which _could_ just be a pointer to an empty array, and hence contiguous (but silly). Same thing could hold for p => a(:,:,n) where n always equals one. Of course, we could also warn unconditionally instead of depending on -Wall. Comments?