Most switches do not like their ports connected like that. Do not do it.
As long as the ports are in separate VLANs (or broadcast domains), there's no reason why this cannot be done, if you do it with ports in the same broadcast domain, you'll end up with a broadcast storm.
Yes, if 802.1q VLANs were so separate as they seem to the innocent bystander. But they aren't, not always, and the FIB in the switch isn't always as simple as it's made out to be.
I've been employed building networks the last 20+ years, and all the people I met who've tried this trick come from it burned: Looping back to the same switch is universally frowned upon. Exception being routed traffic that goes up in a .1q trunk to to a router-on-a-stick and comes back with the source MAC of the router interface, on another VLAN. That's just inefficient, not dangerous,
per se.
It's like building a PSU without proper social distancing between primary and secondary; it might work, but the margin for error is practically gone.
You need a router. A real one, not a NAT toy. It it probably sensible to build a 802.1q trunk to the router from the switch.
You need an IP network per VLAN, and assign the router addresses on these two networks.
Then, computers on those two networks need to get IP addresses on the corresponding network, and also a routing entry that points to where the other network is. If the router is responsible for connectivity to other networks as well, like the Internet, it's probably sufficient to point the default gateway to the router. Do keep in mind that there needs to be routes back to the network too.
This is only required if you wish to have separate networks on the VLANs and also communication between the VLANs - there are different reasons to VLAN (or segment) a network, and there are also occasions when you need to have all the VLANs (segments) on the same ip network - it all depends on what the network "designer" is trying to achieve.
Having the same IP network on different VLANs is IMNSHO counterproductive and violates "rule of least surprise". If someone tried that trick in my network, stern words would be uttered.
I fully appreciate the desire to isolate parts of a network. It is something I do, a lot, but I also make certain that I have an IP address plan and routing set up that makes lack of reachability a policy decision, not a design consequence.
Anyway, we're getting way off on a tangent here. This is not what the OP was trying to achieve, I guess.
There's a "Why do you want this?" question that's not getting asked.
Dear thread starter: Why?