Far from the caricature of being a sleepy, cactus-filled preserve of Midwestern snowbirds with a penchant for golf and dry heat, this booming and increasingly diverse state spotlights the opportunities and pitfalls both parties are grappling with nationally. As Republicans veer between a hard-line primary electorate and a very different one that will show up in November, Democrats have to hold down their losses in the rural areas and win over suburban centrists — who routinely supported Mr. McCain and a procession of Republican presidential nominees. That means offering some measure of support for immigration restrictions without depressing the state’s Latinos.

The roiling dispute over the border and illegal immigration in Arizona, is largely why Mr. Trump won here by less than four percentage points in 2016 and the hard-line county sheriff Joe Arpaio was ousted from his post the same year. A coalition of Hispanics and some right-leaning whites, including many of the state’s Mormons, proved deeply uncomfortable with the incendiary language and scapegoating that is a rhetorical hallmark of both men.

And that split seems baked in to the demography of Arizona, which has the widest so-called racial generation gap of any state in the country: Just 19 percent of the population over 65 is nonwhite; 60 percent of those under 18 are nonwhite.

Those numbers — and the tension between the demands of Republican primary voters and those of the wider electorate — will eventually prove their party’s undoing, some Republicans worry. There is a sharp divide between native-born white Arizonans, who grew up around Hispanics and hearing Spanish, and transplants to the Southwest from more heavily white states.

“The Arizona Republican Party, they’re just slitting their own throats,” said John Giles, the Republican mayor of Mesa, which has a larger population than St. Louis. “They are pushing and doing everything they can to offend the Latino population.”

Mr. Flake, like Mr. Giles, is a native-born Arizonan, and he shares the same alarm about both immigration and embracing Mr. Trump. He is worried that Ms. McSally is going overboard.

“I’d be a lot more careful now in how much you cozy up,” Mr. Flake said of his would-be successor, adding: “There’s a balance she’s got to strike. I cringe a little — I think a lot of people do.”